Question

I read data from a website by json and it works fine . This is my code to return the data and put them into a 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);

the quesetion is ,do I need to recycle them ? is it optimized ? I may want to show an image beside each item and each listView row may have an background image .

Thanks very much

Was it helpful?

Solution

you are using:

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

this is a predefined class by Android, it optimizes everything.

however, if you want to show an image you need to write your custom adapter, because Android doesnt know how to lazy load the images, if you don't tell it how to do that.

so you need to write a custom adapter and override its getView method, and there you will need to recycle your views, otherwise if you have a bunch of images, it will be VERY slow to scroll

the code written inside the SimpleAdapter getView method

  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));
        }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top