我阅读了其他帖子,但没有找到适合我的解决方案。我创建了一个自定义 ListView。此列表中的每一项都是复杂项,包含 ImageView 和两个 TextViews。应用适配器并填满列表后,我想更改 ImageView 中的图像,但我希望这不会发生在点击事件上。
private class ListAdapter extends ArrayAdapter<RSSItem> {
private List<RSSItem> items;
public ListAdapter(Context context, int textViewResourceId, List<RSSItem> items) {
super(context, textViewResourceId, items);
this.items = items;
}
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.list_item, null);
}
RSSItem item = items.get(position);
if (item != null) {
ImageView image = (ImageView) v.findViewById(R.id.ivImage);
TextView title = (TextView) v.findViewById(R.id.tvTitle);
TextView description = (TextView) v.findViewById(R.id.tvDescription);
if (image != null) {
Drawable d = getResources().getDrawable(R.drawable.icon);
image.setTag(item.getImageURL());
image.setBackgroundDrawable(d);
}
if (title != null) {
title.setText(item.getTitle());
}
if (description != null) {
description.setText(item.toString());
}
}
return v;
}
}
我试图获取 Context,但我不知道如何访问正确的 ImageView。 感谢您的帮助!
最佳答案
向您的 RSSItem
类添加一个字段,告诉适配器要显示哪个图像。在您的 AsyncTask.onPostExecute
方法中更改该值,然后调用 ArrayAdapter
的 notifyDataSetChanged
方法。
您的 getView()
覆盖应该使用您放入 RSSItem 中的值来选择要显示的图像。当您调用 notifyDataSetChanged()
时,它会为每个项目再次调用。
关于android - 如何更改 ListView 中的图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6719432/