【问题标题】:How to Convert Hashmap to ArrayList [duplicate]如何将 Hashmap 转换为 ArrayList [重复]
【发布时间】:2018-04-12 08:46:13
【问题描述】:
private void showResult(){
        try {
            JSONObject jsonObject = new JSONObject(mJsonString);
            JSONArray jsonArray = jsonObject.getJSONArray(TAG_JSON);


            for(int i=0;i<jsonArray.length();i++){

                JSONObject item = jsonArray.getJSONObject(i);

                String id = item.getString(TAG_ID);
                HashMap<String,String> hashMap = new HashMap<>();
                hashMap.put(TAG_ID, id);

                String aa = hashMap.get("user_no");

                mArrayList.add(hashMap);
            }

        }
    }

我想将 Hashmap 中的数据转换为 ArrayList 或其他东西

我尝试了其他方法但不能

【问题讨论】:

  • 您还尝试过什么其他方法?
  • 创建一个复合对象,有两个变量:key 和 content。遍历您的地图,并为每个元素创建其键和值的实例,并将它们存储在您的列表中
  • 您的代码似乎创建了一个ArrayList&lt;HashMap&gt;。这不是你想要的吗?
  • 我想将 hashmap 中的数据写入 ArrayList 或其他东西

标签: java json firebase


【解决方案1】:

在下面添加行。

List<HashMap> list = new ArrayList<HashMap>();

HashMap<String,String> hashMap = new HashMap<>();
hashMap.put(TAG_ID, id);

list.add(hashMap)

【讨论】:

  • 恭喜……你只丢失了一半的信息
  • 示例 { 索引 0:TAG_ID = 1,索引 1:TAG_ID = 2,索引 2:TAG_ID =3}
  • 然后将Hashmap添加到ArrayList中
  • list[0]=1 , list[1]=2 , list[2]=3
  • 我编辑了我的答案
猜你喜欢
  • 2011-12-09
  • 2019-11-04
  • 2017-08-14
  • 2012-06-02
  • 2016-06-28
  • 2014-05-15
  • 1970-01-01
  • 2019-03-15
  • 2015-06-01
相关资源
最近更新 更多