使用Array HashMap的onClickListener的Android ListAdapter

时间:2012-09-02 10:39:55

标签: android listview hashmap

我正在考虑使用ListView数组创建HashMap。但我一直无法找到任何使用HashMap数组的示例,我理解使用ListViews的概念,但似乎无法使用HashMap数组合并它。有没有人知道他们可以指出我喜欢的任何例子,在SO上有几个例子,人们只使用HashMap但没有类型数组。

非常感谢你的帮助,这让我疯了两个星期。

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.deals);

    ds = new DealsSQL(DealsUI.this);
    dm = new DealsManage(DealsUI.this);

    //new LoadDeals().execute();

    ArrayList<HashMap<String, String>> inboxList new ArrayList<HashMap<String, String>>();

        ListView listView = (ListView) findViewById(android.R.id.list);
        listView.setAdapter(new HashMapAdapter(inboxList));
        listView.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            }
        });
}

public class HashMapAdapter extends BaseAdapter {

    private ArrayList<HashMap<String, String>> mData = new ArrayList<HashMap<String, String>>();
    private String[] mKeys;
    public HashMapAdapter(ArrayList<HashMap<String, String>> inboxList){
        mData  = inboxList;
        mKeys = mData.keySet().toArray(new String[inboxList.size()]);

    }


    public int getCount() {
        return mData.size();
    }


    public Object getItem(int position) {
        return mData.get(mKeys[position]);
    }


    public long getItemId(int arg0) {
        return arg0;
    }


    public View getView(int pos, View convertView, ViewGroup parent) {
        String key = mKeys[pos];
        String Value = getItem(pos).toString();

        //do your view stuff here

        return convertView;
    }
}

1 个答案:

答案 0 :(得分:0)

您现在正在做的是在每个listitem上检索HashMap。我想(你的代码)你想要做的是,在你的ListView的每一行,fetch&amp;显示HashMap<String,String>的内容。也就是说,这是一个例子。

使用ListView ArrayList创建HashMap<?,?>与其他任何ArrayList没有区别。在getView(...) - 方法中,使用int posgetItem(int pos)方法获取当前项。然后,像往常一样构建视图。

有关ViewHolder模式(和convertView)的更多信息,请检查http://www.vogella.com/articles/AndroidListView/article.html#adapterperformance_hoder

示例:

...

/**
 * Return the HashMap that is stored in your ArrayList at position, instead
 * of just the value.
 */
public HashMap<String, String> getItem(int position) {
    return mData.get(position);
}

...

public View getView(int pos, View convertView, ViewGroup parent) {

    HashMap<String, String> item = getItem(pos);
    ViewHolder holder;        

    if(convertView == null) {
        // ConvertView is re-used by ListView. That means we don't have to re-inflate
        // it on every row. Instead, if convertView is already set we just reuse it.
        // To prevent having to call .findViewById( id ) every time, we use the pattern
        // called the `ViewHolder`-pattern.
        convertView = LayoutInflater.from(mContext).inflate(R.layout.list_item);

        holder = new ViewHolder();
        holder.textview1 = convertView.findViewById(R.id.textview1);

        convertView.setTag(holder);

    } else {
        holder = convertView.getTag();
    }

    // now, we have both a populated convertView and a reference to it's individual subviews
    // in the form of holder.

    // manipulate the views in your listitem here
    // I assume you want to display the contents of the HashMap<String,String> here
    // so convert the HashMap to something iterable, like EntrySet
    for(Map.Entry<String, String> entry : map.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();
            // notice how we set the TextView attributes by accessing holder.textview1
            holder.textview1.setText( key + " => " + value + "\n");
    }


    return convertView;
}

private class ViewHolder {
    public TextView textview1;
}

注意:

如果您只想为每个HashMap条目显示一个listitem,我认为您最好的方法是使用hashmap.entrySet().toArray()将其转换为数组并将该数组提供给构造函数中的HashMapAdapter,这将给出您的Map.Entry数组为mDataset。在getView(...) - 方法中,您只需使用item.getKey()item.getValue(),而不是循环浏览数据项。此外,您的getItem(position) - 方法必须返回Map.Entry类型而不是HashMap<String, String>