[英]ListView with custom layout for each row
我為每行設置一個帶有布局XML的listview:
this.setContentView(R.layout.item_view);
ListView m_ListView = this.getListView();
m_ListView.setTextFilterEnabled(true);
m_ListView.setItemsCanFocus(false);
m_ListView.setCacheColorHint(0x00000000);
mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ArrayList<View> views=new ArrayList<View>();
e = mInflater.inflate(R.layout.item_first, null);
views.add(e);
r = mInflater.inflate(R.layout.item_second, null);
views.add(r);
mAdapter = new SillyAdapter(views);
setListAdapter(mAdapter);
現在例如,我在item_first
有一個TextView
,我想從代碼中進行設置:
TextView name = (TextView)findViewById(R.id.textView1);
name.setText(m_item.name);
但是名字總是空的。知道為什么會發生嗎?
您必須在代碼中進行以下更改。
TextView name = (TextView)e.findViewById(R.id.textView1);
name.setText(m_item.name);
由於從錯誤的視圖調用findViewById方法或可能是從活動調用的,因此名稱為null。 因為你膨脹如下
e = mInflater.inflate(R.layout.item_first, null);
您必須在展開視圖e上調用findViewById。 這樣您將獲得包含在xml R.layout.item_first中的 ID為R.id.textView1的textview實例
希望這會有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.