I have:
- One
MyListActivity class extends ListActivitysetContentView(R.layout.mylist); - Two xml files:
mylist.xmlandlist_row.xml. mylist.xmlis the layout andlist_row.xmlis how each row looks like.list_row.xmlcontains 3TextViews(t1,t2,t3).
I want to change some text of t2 in MyListActivity class. Because the content view is mylist.xml so I can’t simply use findViewById. So I used LayoutInflater.
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.list_row, null);
textView2 = (TextView) v.findViewById(R.id.t2);
textView2.setText("ccccc");
The problem is that the text of t2 is never changed. I have tried many times and the text remains the text I set in the list_row.xml. I can’t figure out why.. Could someone help please. Thanks!
=====Solution:=====
Create my own SimpleCursorAdapter class and override the getView() method.
private class MySimpleCursorAdapter extends SimpleCursorAdapter {
Cursor c;
public MySimpleCursorAdapter(Context context, int layout, Cursor c,
String[] from, int[] to) {
super(context, layout, c, from, to);
this.c = c;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
super.getView(position, convertView, parent);
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.list_row, parent, false);
TextView textview1 = (TextView) rowView.findViewById(R.id.text1);
textView1.setText(c.getString(1));// whatever should be in textView1
textView2 = (TextView) rowView.findViewById(R.id.text2);
textView2.setText("ccccc");
return rowView;
}
}
The super.getView(position, convertView, parent); is quite important because if I don’t have that line, textView1 will always show the value of first row(e.g. if textView1 shows the id, then this is always 1).
If you want to change the
TextViewtext you will have to do it in the adapter of theListView.