【发布时间】:2014-09-08 19:21:48
【问题描述】:
我有两个ArrayList<String>,我想在一个ListView 中按顺序并排显示它们。
我已经设置了一个自定义的“row”、ListView,并了解如何使用list.setAdapter(myAdapter)。
我的问题是myAdaptor 不起作用。
如何修复这个ArrayAdaptor 以显示两个ArrayList<String>?
CustomArrayAdaptor.java
public class CustomArrayAdapter extends ArrayAdapter<Words> {
private ArrayList<Words> arrayOne;
private ArrayList<Words> arrayTwo;
public CustomArrayAdapter(Context context, int textViewResourceId, ArrayList<Words> arrayOne, ArrayList<Words> arrayTwo) {
super(context, textViewResourceId, arrayOne, arrayTwo);
this.arrayOne = arrayOne;
this.generates = arrayTwo;
}
public View getView(int position, View convertView, ViewGroup parent){
View v = convertView;
if (v == null) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.list_item, null);
}
Words a = arrayOne.get(position);
Words g = arrayTwo.get(position);
if (a != null) {
TextView arrayOne = (TextView) v.findViewById(R.id.text_view_1);
TextView arrayTwo = (TextView) v.findViewById(R.id.text_view_2);
}
return v;
}
}
【问题讨论】:
-
您从未在适配器的 getView 方法中实际设置 TextViews arrayOne 和 arrayTwo 上的文本,这就解释了为什么您什么都看不到。此外,您的适配器中不需要有 2 个 ArrayList
,因为您的 Word 对象包含来自您的 ArrayList 的对字符串的引用(我假设)。 -
好的,我将删除
ArrayList<Word>和 setText 为 "" + arrayOne.size() 看看是否有效 -
查看下面我的回答以获取更多信息stackoverflow.com/a/24812054/1426565
-
将这两行剪切粘贴到 if(v == null) TextView arrayOne = (TextView) v.findViewById(R.id.text_view_1); TextView arrayTwo = (TextView) v.findViewById(R.id.text_view_2);
标签: android listview arraylist android-arrayadapter