我是否需要回收此listView

时间:2014-05-11 05:41:16

标签: android json android-listview android-adapter

我通过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行可能有一个背景图像。

非常感谢

1 个答案:

答案 0 :(得分:0)

你正在使用:

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));
        }
    }