【问题标题】:Do I need to recycle this listView我需要回收这个 listView
【发布时间】:2014-05-11 05:41:16
【问题描述】:

我通过 json 从网站读取数据,它工作正常。 这是我返回数据并将它们放入 listView 的代码。

        contactList = new ArrayList<HashMap<String, String>>();
        ......
        ListAdapter adapter = new SimpleAdapter(getActivity(), contactList,
                R.layout.spots_tab3_json_listitem,
                new String[] { TAG_Message }, new int[] { R.id.message });

        lv.setAdapter(adapter);

问题是,我需要回收它们吗?优化了吗?我可能想在每个项目旁边显示一个图像,并且每个 listView 行可能有一个背景图像。

非常感谢

【问题讨论】:

标签: android json android-listview android-adapter


【解决方案1】:

您正在使用:

ListAdapter adapter = new SimpleAdapter(getActivity(), contactList,
                R.layout.spots_tab3_json_listitem,
                new String[] { TAG_Message }, new int[] { R.id.message });

这是 Android 预定义的类,它优化了一切。

但是,如果您想显示图像,则需要编写自定义适配器,因为 Android 不知道如何延迟加载图像,如果您不告诉它如何做到这一点。

所以你需要编写一个自定义适配器并覆盖它的 getView 方法,然后你需要回收你的视图,否则如果你有一堆图像,滚动会很慢

SimpleAdapter getView 方法中编写的代码

  public View getView(int position, View convertView, ViewGroup parent) {
        return createViewFromResource(position, convertView, parent, mResource);
    }

private View createViewFromResource(int position, View convertView,
        ViewGroup parent, int resource) {
    View v;
    if (convertView == null) {
        v = mInflater.inflate(resource, parent, false);
    } else {
        v = convertView;
    }

    bindView(position, v);

    return v;
}



private void bindView(int position, View view) {
    final Map dataSet = mData.get(position);
    if (dataSet == null) {
        return;
    }

    final ViewBinder binder = mViewBinder;
    final String[] from = mFrom;
    final int[] to = mTo;
    final int count = to.length;

    for (int i = 0; i < count; i++) {
        final View v = view.findViewById(to[i]);
        if (v != null) {
            final Object data = dataSet.get(from[i]);
            String text = data == null ? "" : data.toString();
            if (text == null) {
                text = "";
            }

            boolean bound = false;
            if (binder != null) {
                bound = binder.setViewValue(v, data, text);
            }

            if (!bound) {
                if (v instanceof Checkable) {
                    if (data instanceof Boolean) {
                        ((Checkable) v).setChecked((Boolean) data);
                    } else if (v instanceof TextView) {
                        // Note: keep the instanceof TextView check at the bottom of these
                        // ifs since a lot of views are TextViews (e.g. CheckBoxes).
                        setViewText((TextView) v, text);
                    } else {
                        throw new IllegalStateException(v.getClass().getName() +
                                " should be bound to a Boolean, not a " +
                                (data == null ? "<unknown type>" : data.getClass()));
                    }
                } else if (v instanceof TextView) {
                    // Note: keep the instanceof TextView check at the bottom of these
                    // ifs since a lot of views are TextViews (e.g. CheckBoxes).
                    setViewText((TextView) v, text);
                } else if (v instanceof ImageView) {
                    if (data instanceof Integer) {
                        setViewImage((ImageView) v, (Integer) data);                            
                    } else {
                        setViewImage((ImageView) v, text);
                    }
                } else {
                    throw new IllegalStateException(v.getClass().getName() + " is not a " +
                            " view that can be bounds by this SimpleAdapter");
                }
            }
        }
    }
}




 public void setViewImage(ImageView v, String value) {
        try {
            v.setImageResource(Integer.parseInt(value));
        } catch (NumberFormatException nfe) {
            v.setImageURI(Uri.parse(value));
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-18
    • 2015-04-06
    • 2022-07-20
    • 1970-01-01
    • 1970-01-01
    • 2018-01-18
    • 2020-03-06
    • 1970-01-01
    相关资源
    最近更新 更多