我希望制作相同长度的2 ArrayList<Strings>
个,在1 ListView
Word 1是TextView
,应该填充1 ArrayList<String>
而Word 2是另一个TextView
,应填充不同的ArrayList<String>
*这些 TextViews
,不 Button
s
我的ArrayAdaptor
应该是什么样的?
如果这是重复,请发给我一个链接。
CustomArrayAdapter.java,双方显示相同的ArrayList<String>
(不是我需要的)
public class CustomArrayAdapter extends BaseAdapter {
Context context;
private ArrayList<String> words1;
private ArrayList<String> words2;
public CustomArrayAdapter(Context context, ArrayList<String> words1, ArrayList<String> words2) {
this.words1 = words1;
this.generates = words2;
this.context = context;
}
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.list_item, null);
}
String a = (String) getItem(position);
String b = (String) getItem(position);
TextView tv1 = (TextView) v.findViewById(R.id.rhymed_word);
TextView tv2 = (TextView) v.findViewById(R.id.generated_word);
tv1.setText(a);
tv2.setText(b);
return v;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return words1.size();
}
@Override
public Object getItem(int position) {
if (position >= words1.size())
return words2.get(position);
return words1.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
}
答案 0 :(得分:0)
好的,不要使用重写方法,只需执行以下操作:
TextView tv1 = (TextView) v.findViewById(R.id.rhymed_word);
TextView tv2 = (TextView) v.findViewById(R.id.generated_word);
tv1.setText(words1.get(position));
tv2.setText(words2.get(position));
return v;
答案 1 :(得分:0)
根据您的描述,您希望显示一个ArrayList中的word1和另一个ArrayList中的word2。 ArrayAdapter在您的情况下不起作用,因为您要为每一行显示两个数组。一种方法是从BaseAdapter派生一个类并实现getView()。以下是类实现的一部分:
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View v = convertView;
if(v == null) {
// Inflate a layout for your listview row
}
// Assuming the 2 arrays are words1 and words2
// Assuming the textviews are word1 and word2
word1.setText(words1[position]);
word2.setText(words2[position]);
return v;
}
@Override
public int getCount()
{
return words1.size();
}