TreeMap to ListView in Android

懵懂的女人 提交于 2019-12-02 09:57:07
Skynet

I finally made a custom Adapter, took reference from here:

1) What adapter shall I use to use HashMap in a ListView

2) http://www.vogella.com/articles/AndroidListView/article.html.

3) http://www.youtube.com/watch?v=wDBM6wVEO70

4) https://dl.google.com/googleio/2010/android-world-of-listview-android.pdf

Edit: Based on suggestions in the comments, I have invoked a ViewHolder pattern to handle recycling of views and address the concerns of memory management and battery use. The complete code is as follows:

public class TreeMapAdapter extends ArrayAdapter<String> {
    private final Context context;
    private final String[] values;
    String[] mValues;


    private TreeMap<String, String> mData = new TreeMap<String, String>();
    private String[] mKeys;

    public TreeMapAdapter(Context context, String[] mKeys,TreeMap<String, String> data) {
        super(context, R.layout.chklistlayout, mKeys);
        this.context = context;
        this.values = mKeys;
        mData  = data;
        mValues = mData.values().toArray(new String[data.size()]);
    }
    public int getCount() {

        return mData.size();

    }

    public String getItem(int position) {

        return mData.get(mKeys[position]);
    }


    public long getItemId(int arg0) {

        return arg0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {


        if (convertView == null) { 
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.chklistlayout, 
                    parent, false);
            holder = new ViewHolder(); 
            holder.textView = (TextView) convertView.findViewById(R.id.textView1); 
            holder.imageview = (TextView) convertView.findViewById(R.id.textView2); 
            holder.CB = (CheckBox) convertView.findViewById(R.id.checkBox1); 

            convertView.setTag(holder); 
        } else { 
            holder = (ViewHolder) convertView.getTag(); 
        }
        holder.textView.setText(values[position]);
        holder.imageview.setText(mValues[position]);


        String s = mValues[position];
        if (s.contains("h")) {
            holder.CB.setChecked(true);
        } else {

        }

        return convertView;
    }
} 

Following is the holder class:

static class ViewHolder {

    TextView textView, imageview;
    CheckBox CB;


}

There are three ways to recycle views, one is called as the Dumb way, the other as the right way and the one used in this example is the fastest way. Users who stumble on this post later, I highly recommend to go through the YouTube video. Its worth it!

In a nutshell ConvertView is responsible for recycling of views, it keeps track of all the views which are moving out of screen focus and returns one view which is recycled and reused.

Lastly Thanks to KMDev, who sparked this thought of researching more and making a template for Holder pattern.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!