【问题标题】:How to pass multiple HashMap<String,Integer> in BaseAdapter如何在 BaseAdapter 中传递多个 HashMap<String,Integer>
【发布时间】:2016-02-01 11:20:42
【问题描述】:

我在我的列表视图中传递一个 HashMap BaseAdapter 工作得很好。

当我在适配器中传递三个 HashMap 时,它给出的值与以前相同

我如何传递多个我不知道的 HashMap

这是我的适配器

MyAdapter.java

    public class MyAdapter extends BaseAdapter {
    private final ArrayList mData;

    public MyAdapter(HashMap<String, Integer> name) {
        mData = new ArrayList();
        mData.addAll(name.entrySet());
    }

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

    @Override
    public Map.Entry<String, Integer> getItem(int position) {
        return (Map.Entry) mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO implement you own logic with ID
        return 0;
    }

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

        if (convertView == null) {
            result = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_order_list, parent, false);
        } else {
            result = convertView;
        }

        Map.Entry<String, Integer> item = getItem(position);
        String value = String.valueOf(item.getValue());

        // TODO replace findViewById by ViewHolder
        ((TextView) result.findViewById(R.id.Name)).setText(item.getKey());
        ((TextView) result.findViewById(R.id.Quantity)).setText(value);


        return result;
    }
}

提前感谢您的帮助

【问题讨论】:

  • 你想做什么?
  • 我想做一个列表视图
  • 这是什么意思?我怎么能通过多个我不知道的适配器!!
  • 检查下面的答案。

标签: android listview hashmap


【解决方案1】:

当我在适配器中传递三个 HashMap 时,它给出的值与以前相同

由于您没有向数据集添加内容的方法,您可能会调用new MyAdapter 三次,并将最后一个实例传递给您的ListView。您可能想要添加一个方法来将新的HashMap 的实例添加到mData,例如

 public void addItem(HashMap<String, Integer> item) {
    synchronized(mData) {
       mData.add(item)
    }
    notifyDataSetChanged();
 }

并使用Adapter 的实例从外部调用它

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-26
    • 2016-06-11
    相关资源
    最近更新 更多