Reputation: 7900
I setup a listview with a layout XML for each row:
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);
now for example i have a TextView
in item_first
and i want to set it from the code:
TextView name = (TextView)findViewById(R.id.textView1);
name.setText(m_item.name);
but name is always null.any idea why it happen?
Upvotes: 0
Views: 142
Reputation: 3882
You have to do the following changes in your code.
TextView name = (TextView)e.findViewById(R.id.textView1);
name.setText(m_item.name);
You are getting name as null since you are calling findViewById method from wrong view or might be calling from activity. since you are inflating as follows
e = mInflater.inflate(R.layout.item_first, null);
you have to call findViewById on inflated view e. so that you will get the instance of textview with id R.id.textView1 contained in xml R.layout.item_first
Hope this will help.
Upvotes: 1