2012-07-18 55 views
4

我已經創建了一個動態LinearLayout,我想將這些佈局添加到ListView。代碼如下:如何將我的動態佈局添加到ListView?

DynamicLayout代碼:

import android.content.Context; 
import android.widget.LinearLayout; 
import android.widget.TextView; 

public class DynamicContentLayout extends LinearLayout{ 
    private Context context; 
    private int linearLayoutMargin; 

    public DynamicContentLayout(Context context) { 
     super(context); 
     this.context = context; 
     linearLayoutMargin = (int) context.getResources().getDimension(R.dimen.xlarge_margin_padding_fixed); 
     initViews(); 
    } 

    private void initViews() { 
     LinearLayout.LayoutParams layout_lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
     layout_lp.setMargins(linearLayoutMargin, linearLayoutMargin, linearLayoutMargin, linearLayoutMargin); 
     this.setLayoutParams(layout_lp); 
     this.setOrientation(LinearLayout.VERTICAL); 
     this.addView(headerTitle("My Text")); 

    } 

    private TextView headerTitle(String title) { 
     TextView txtView = new TextView(context); 
     txtView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 
     txtView.setPadding(6, 6, 6, 6); 
     txtView.setText(title); 
     txtView.setBackgroundColor(context.getResources().getColor(R.color.red)); 
     txtView.setTextColor(context.getResources().getColor(R.color.white)); 
     return txtView; 
    } 

} 

ListAdpaterCode:

import java.util.ArrayList; 
import android.content.Context; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 

public class ListViewAdapter extends ArrayAdapter<DynamicContentLayout> { 

    private Context context; 
    private ArrayList<DynamicContentLayout> layoutList = null; 

    public ListViewAdapter(Context context, int textViewResourceId, ArrayList<DynamicContentLayout> layoutList) { 
     super(context, textViewResourceId, layoutList); 
     this.context = context; 
     this.layoutList = layoutList; 
    } 

    @Override 
    public int getCount() { 
     return layoutList.size(); 
    } 

    @Override 
    public DynamicContentLayout getItem(int position){ 
     return layoutList.get(position); 
    } 

    @Override 
    public long getItemId(int position) { 
     return position; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     DynamicContentLayout entry = (DynamicContentLayout) layoutList.get(position); 
     convertView = (View) entry; 
     return convertView; 
    } 
} 

最後的代碼在MainActivity我在哪裏設置適配器:

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    ListView listView = (ListView) findViewById(R.id.listView); 
    ArrayList<DynamicContentLayout> contents = new ArrayList<DynamicContentLayout>(); 
    contents.add(new DynamicContentLayout(getApplicationContext())); 
    contents.add(new DynamicContentLayout(getApplicationContext())); 
    contents.add(new DynamicContentLayout(getApplicationContext())); 
    ArrayAdapter<DynamicContentLayout> arrayAdapter = new ArrayAdapter<DynamicContentLayout>(getApplicationContext(), android.R.layout.simple_list_item_1, contents); 
    listView.setAdapter(arrayAdapter); 
    arrayAdapter.notifyDataSetChanged(); 
} 

的Xml佈局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
> 

    <ListView 
     android:id="@+id/listView" 
     android:layout_width="fill_parent" 
     android:background="@color/blue" 
     android:layout_height="fill_parent" 
     android:alwaysDrawnWithCache="true" 
     android:clickable="true" 
     android:layout_alignParentTop="true" 
     android:divider="@color/grey" 
     android:drawingCacheQuality="auto" 
     android:fastScrollEnabled="true" 
     android:paddingLeft="15dp" 
     android:paddingRight="15dp" 
     android:scrollbars="none" > 
    </ListView> 

</RelativeLayout> 

現在,當我運行時,沒有錯誤報告。但我看不到listItems佈局。我能看到的只是班級的名字。例如:「[email protected]0」作爲listItem的行而不是Layout。

還有一點我想補充。我無法使用XMl佈局,因爲我的DynamicLinearLayout中的視圖可以增加/減少某些行。

請提出建議。

謝謝。

回答

0

您使用ArrayAdapter爲您的適配器爲ListView,而不是你自己的ListViewAdapter延伸ArrayAdapter< DynamicContentLayout>

你現在看到的是你的對象的ToString表示,因爲,那是一個ArrayAdapter做什麼。

+0

我將其更改爲 ListViewAdapter arrayAdapter = new ListViewAdapter(getApplicationContext(),android.R.layout.simple_list_item_2,contents); \t \t listView.setAdapter(arrayAdapter); \t \t arrayAdapter.notifyDataSetChanged(); 現在我得到錯誤。 – Debopam 2012-07-18 07:29:15

+0

如果你不告訴我哪個錯誤 – 2012-07-18 07:32:06

+0

好吧,幫不了你。等待..它在我的LogCat ..讓我把它給你。 – Debopam 2012-07-18 07:34:01

0

你有自定義listViews的概念都錯了。

您應該在xml佈局中定義您的自定義佈局。 陣列適配器的類型不應該是佈局。它應該是你試圖用listView填充的對象類型。

查看此tutorial以更好地瞭解您應該執行的步驟。

+0

我必須同意這一點 – 2012-07-18 07:40:28