TreeMap中来的ListView在Android中 [英] TreeMap to ListView in Android

查看:125
本文介绍了TreeMap中来的ListView在Android中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做Android的一个清单类型的应用程序,其中我在一个列表视图,并且旁边有一个复选框填充的项目。当用户点击一个项目,我希望它剔除文字和更新相关的列在数据库中的条目。出于这个原因,我存储在TreeMap的名称 - 值对。凡名称对应于数据库列名称和值在列名的值。我已经尝试使用下面的做到了这一点:

I am making a checklist type app in Android, in which I am populating items in a list view with a check box next to them. when a user clicks on an item I want it to strike out the text and update the entry in the database for the relevant column. For this reason I am storing Name-Value pairs in a TreeMap. Where Name corresponds to the Database Column Name and Value to the value in that Column name. I have achieved this using the following:

if ((c.getString(c.getColumnIndexOrThrow("Field130"))).length() > 3) {
                    String D11 = c.getString(c.getColumnIndexOrThrow("Field130"));
                    map.put("Field130", D11); 
                }

现在我要在相应的列表视图和价值在第二TextView中的一个TextView的显示名称。我试过如下:

Now I want to display the name in one textview of the corresponding listview and value in the second textview. I tried the following:

ArrayList<TreeMap<String, String>> mylist = new ArrayList<TreeMap<String, String>>();
            mylist.add(map);
            ListView list = (ListView) findViewById(R.id.list);
            ArrayList<String> list1 = new ArrayList<String>(map.values());
            dataAdapter = new ArrayAdapter<String>(this,R.layout.chklistlayout,R.id.textView1, list1);
            list.setAdapter(dataAdapter);

不过,我不能够得到想要的结果,因为我只得到显示的值。难道就没有办法,我可以直接映射我的TreeMap到ListView?我打开的思想,为更强大的,即使这意味着返工我的整个code。

However I am not able to get the desired results, as I get only the Values displayed. Is there no way that I can map my TreeMap directly to the listview? I am open to ideas which are more robust, even if that means reworking my whole code.

推荐答案

我终于做了一个自定义的适配器,参考了从这里开始:

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

1)<一href="http://stackoverflow.com/questions/5234576/what-adapter-shall-i-use-to-use-hashmap-in-a-listview">What适配器应我使用使用HashMap的在ListView

2)<一href="http://www.vogella.com/articles/AndroidListView/article.html">http://www.vogella.com/articles/AndroidListView/article.html.

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

4)<一href="https://dl.google.com/googleio/2010/android-world-of-listview-android.pdf">https://dl.google.com/googleio/2010/android-world-of-listview-android.pdf

编辑:基于在评论中建议,我已经调用的ViewHolder模式来处理回收的观点和解决内存管理和电池使用的关注。完整的code是如下:

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

以下是holder类:

Following is the holder class:

static class ViewHolder {

    TextView textView, imageview;
    CheckBox CB;


}

有三种方法回收观点,一种被称为阿呆方式,其它如以正确的方式,并在这个例子中使用的是最快的方式。对这个职位谁绊倒后的用户,我强烈建议要经过的YouTube视频。这很值得!

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!

在简而言之ConvertView负责回收的观点,它使所有这些迁出的屏幕焦点的看法轨道并返回被回收再利用一个视图。

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.

最后感谢KMDev,谁引发了这一思想研究更多的,以及为持有人模式的模​​板。

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

这篇关于TreeMap中来的ListView在Android中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆