0

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

非常感谢您的帮助,这让我连续两个星期都发疯了。

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

1 回答 1

0

您现在正在做的是在每个列表项上检索一个 HashMap。我假设(根据您的代码)您想要做的是,在您的每一行中ListView,获取并显示您的HashMap<String,String>. 也就是说,这是一个例子。

用of创建 aListView与其他任何. 在您的-method 中,使用和方法获取当前项目。然后,像往常一样构建您的视图。ArrayListHashMap<?,?>ArrayListgetView(...)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 条目显示一个列表项,我认为最好的办法是将其转换为一个数组,hashmap.entrySet().toArray()并在构造函数中将该数组提供给您的 HashMapAdapter,这将为您提供Map.Entry一个mDataset. 在您的- 方法中,您getView(...)无需遍历数据项,而是使用item.getKey()and item.getValue()。此外,您的getItem(position)-method 必须返回 typeMap.Entry而不是HashMap<String, String>.

于 2012-09-02T11:42:07.760 回答