2012-03-31 17 views
0

我設置自定義佈局與佈局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); 

但是名字總是空的。任何想法爲什麼會發生?

+2

你可以發佈SillyAdapter的代碼嗎?您是否在SillyAdapter中實現了getView? – Jeroen 2012-03-31 10:36:52

回答

1

您必須在代碼中進行以下更改。

TextView name = (TextView)e.findViewById(R.id.textView1); 
name.setText(m_item.name); 

由於您從錯誤視圖調用findViewById方法,或者可能正在從活動調用,因此您的名稱爲null。 因爲你充氣如下

e = mInflater.inflate(R.layout.item_first, null); 

必須調用findViewById上充氣視圖ë。這樣你會得到的TextView的實例ID爲R.id.textView1包含在XML R.layout.item_first

希望這會有所幫助。

+0

是的,它在那裏,thx的幫助 – MTA 2012-03-31 10:56:24

相關問題