Android:用于处理多个 ArayStringLists 的自定义适配器 [英] Android: custom adapter for handling more than one ArayStringLists
问题描述
我有一个 Row 布局,有两个 textView,比如说 textViewX 和 textViewY.我知道如何使用适配器用一个 arrayList 填充一个 textView.
I have a Row layout, with two textViews, lets say textViewX and textViewY. I know how to populate one textView with one arrayList using the Adapter.
如何制作自定义适配器来处理两个数组字符串列表以填充包含两个 textView 的 row_layout ?
How do I make a custom Adapter to handle two Array String Lists to populate a row_layout containing two textViews ?
我尝试这样做:
// 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();
}
应用程序崩溃了,我检查了日志,有一个致命错误"指向这个:
The application crashes, and I checked the logs, and there is a "FATAL ERROR" pointing towards this:
yearTextView.setText((CharSequence) yearStringList);
推荐答案
使用模型类
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 循环填充列表.下面只是一个例子.只需确保填写列表即可.
Now populate the list say using for loop. Below is just an example. Just make sure you populate the list.
for(int i=0;i<10;i++)
{
Model model = new Model();
model.setValue1("value1");
model.setValue2("value2");
aa.add(model);
}
将列表 aa
传递给 CustomAdapter 并在那里使用它
Pass list aa
to CustomAdapter and use it there
在获取视图中
Model mo = (Model) aa.get(position);
tv1.setText(mo.getValue1());
tv2.setText(mo.getValue2());
这篇关于Android:用于处理多个 ArayStringLists 的自定义适配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!