我有一个扩展了ArrayAdapter<String>
的类,以针对不同的行显示不同的图像,我不确定这是正确的方法。现在我的问题是:如何显示ArrayList中的项目,图像显示正常,但是我无法设置文本。然后在一个TextView中显示图标和文本。
class MyAdapter extends ArrayAdapter<String> {
Context context;
int layoutResourceId;
ArrayList<String> list_text;
public MyAdapter(Context context, int layoutResourceId, ArrayList<String> list_text) {
super(context, layoutResourceId, list_text);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.list_text = list_text;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
MyItemHolder holder = null;
if(row == null) {
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new MyItemHolder();
holder.iconView = (TextView)row.findViewById(R.id.item);
holder.textView = (TextView)row.findViewById(R.id.item);
row.setTag(holder);
}
else {
holder = (MyItemHolder)row.getTag();
}
int d = R.drawable.picture;
?ArrayList<String> text = list_text;
holder.iconView.setCompoundDrawablesWithIntrinsicBounds(d, 0, 0, 0);
?holder.textView.setText(text);
return row;
}
static class MyItemHolder {
TextView iconView;
TextView textView;
}
}
最佳答案
String text = list_text.get(position);
holder.textView.setText(text);
关于android - arrayadapter setText,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12769203/