android - 如何更改 ListView 中的图像?

标签 android image listview

我阅读了其他帖子,但没有找到适合我的解决方案。我创建了一个自定义 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 方法中更改该值,然后调用 ArrayAdapternotifyDataSetChanged 方法。

您的 getView() 覆盖应该使用您放入 RSSItem 中的值来选择要显示的图像。当您调用 notifyDataSetChanged() 时,它会为每个项目再次调用。

关于android - 如何更改 ListView 中的图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6719432/

相关文章:

android - *.ics 文件到 android 中的谷歌日历

image - OpenGL 图像加载/存储是否需要手动将 sRGB 转换为线性?

android - ListView PerformItemClick 不工作

delphi - 如何在 ListView 中省略空单元格的网格线?

java - 具有 3 个 TextView 和 1 个单选按钮的动态 ListView。这是可能的?

android - android :windowBackground and android:colorBackground? 之间的区别

android - 在 Android 中,我如何找出在运行时处理 Intent 的类(logcat 很好)

Android 应用发布 list

html - 正斜杠在 html img 标签中做什么

html - 相对 HTML 图像引用 (LAMP)