我有一个Row布局,有两个textViews,比如说textViewX和textViewY。 我知道如何使用适配器用一个arrayList填充一个textView。
如何使自定义适配器处理两个数组字符串列表以填充包含两个textView的row_layout?
我试过这样做:
// Getting the Text view from the ROW_Layout for Year and adding the text from array string lists
TextView yearTextView = (TextView) findViewById(R.id.editTextYear);
try {
yearTextView.setText((CharSequence) yearStringList);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
yearTextView.setTextColor(0xff444444); // Setting Dark Grey color to text
// Getting the Text view from the ROW_Layout for Country and adding the text from array string lists
TextView countryTextView = (TextView) findViewById(R.id.editTextCountry);
try {
countryTextView.setText((CharSequence) countryStringList);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
应用程序崩溃了,我检查了日志,并且有一个“致命错误”指向:
yearTextView.setText((CharSequence) yearStringList);
答案 0 :(得分:1)
使用模型类
class Model
{
String value1;
String value2
public void setValue1(String value1)
{
this.value1=value1;
}
public void setValue2(String value2)
{
this.value2=value2;
}
public String getValue1()
{
return this.value1;
}
public String getValue2()
{
return this.value2;
}
}
现在进入活动
ArrayList<Model> aa = new ArrayList<Model>();
现在填充列表说使用for循环。以下只是一个例子。只需确保填充列表。
for(int i=0;i<10;i++)
{
Model model = new Model();
model.setValue1("value1");
model.setValue2("value2");
aa.add(model);
}
将列表aa
传递给CustomAdapter并在那里使用
在getView中
Model mo = (Model) aa.get(position);
tv1.setText(mo.getValue1());
tv2.setText(mo.getValue2());