у меня есть:
- Один
MyListActivity class extends ListActivity
setContentView(R.layout.mylist)
;
- Два XML-файла:
mylist.xml
и list_row.xml
.
mylist.xml
- это макет, а list_row.xml
- как выглядит каждая строка.
list_row.xml
содержит 3 TextViews(t1,t2,t3)
.
Я хочу изменить текст t2 в классе MyListActivity
. Поскольку представление содержимого - mylist.xml, поэтому я не могу просто использовать findViewById
. Поэтому я использовал 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");
Проблема в том, что текст t2 никогда не изменяется. Я пробовал много раз, и текст остается текстом, который я установил в list_row.xml
. Я не могу понять, почему .. Может кто-нибудь помочь, пожалуйста. Спасибо!
===== Решение : =====
Создайте свой собственный класс SimpleCursorAdapter
и переопределите метод getView()
.
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;
}
}
Значение super.getView(position, convertView, parent);
очень важно, потому что если у меня нет этой строки, textView1
всегда будет показывать значение первой строки (например, если textView1
показывает идентификатор, то это всегда 1).