【问题标题】:How to get the index of the hash map array list in android?如何在android中获取hash map数组列表的索引?
【发布时间】:2015-08-25 04:46:34
【问题描述】:

如何获取哈希映射数组列表的索引,这是我的代码

ArrayList<HashMap<String, String>> branch_list=new  ArrayList<HashMap<String,String>>();
    branch_list.clear();

    Log.d("kkk", "sew1="+Constants.questions_arr_list+" size="+Constants.questions_arr_list.size());
    Cursor cs1 = db.rawQuery("SELECT * from questions WHERE page_sequence='"+onValue+"' and survey_id='"+Constants.survey_id+"' ORDER BY question_sequence ASC",null);
    if (cs1.moveToFirst()) {
        do {
            if (cs1.getString(27).matches("0")) {
                HashMap<String, String> map=new HashMap<String, String>();
                map.put("id", "" + cs1.getString(0));
                map.put("survey_id", "" + cs1.getString(25));
                map.put("title", "" + cs1.getString(12));
                map.put("type_id", "" + cs1.getString(24));
                map.put("required", "" + cs1.getString(7));
                map.put("others", cs1.getString(53));
                map.put("page_yes",  cs1.getString(1));
                map.put("page_no", cs1.getString(2));
                map.put("page_dontno", ""+cs1.getString(54));
                map.put("branching_status", ""+cs1.getString(55));

                branch_list.add(map);
                Constants.questions_arr_list.add(map);
                Constants.questions_arr_list_copy.add(map);

            } else {

            }
            if (cs1.getString(55).matches("1")) {
                break;
            }
        } while (cs1.moveToNext());

    } else {

    } 

下面是我获取值后的数组列表。我想获取 id=2572 的索引, 比如答案=2。

[{id=2570, title=第 1 页问题 01, others=0, page_yes=0, branching_status=0, page_no=0, required=0,survey_id=592, type_id=1, page_dontno=0}, { id=2571, title=第1页问题02, others=0, page_yes=0, branching_status=0, page_no=0, required=0,survey_id=592, type_id=9, page_dontno=0}, {id=2572, title=第 1 页问题 03 分支问题, others=0, page_yes=2, branching_status=1, page_no=3, required=0,survey_id=592, type_id=8, page_dontno=0}, {id=2575, title=Page 2 Question 01, others=0, page_yes=0, branching_status=0, page_no=0, required=0,survey_id=592, type_id=23, page_dontno=0}, {id=2576, title=Page 2 Question 02 Branching Question, others=0, page_yes=5, branching_status=1, page_no=0, required=0,survey_id=592, type_id=9, page_dontno=0}]

【问题讨论】:

  • 为什么HashMap需要索引?地图使用“键”来访问数据,而列表将允许您基于索引进行查找。
  • 看起来你需要遍历你的列表。意味着通过循环读取每个列表项。

标签: android arrays hashmap


【解决方案1】:

如果我理解正确,您想将 Hashmap 的 ID 链接到 ArrayList 的索引?

您可以做的是创建一个新的 HashMap,并将键作为 ID,将值作为索引。

HashMap<String, Integer> index = new new HashMap<String, Integer>();
// for every new ID you add to your existing map, also do the following
index.put(newID, curIndex)
// Let curIndex be a int variable that you increment everytime after you add a new element to your map

希望对你有帮助

注意:您还必须将现有的 HashMap 更改为 LinkedHashMap,因为顺序至关重要

【讨论】:

    【解决方案2】:

    您可以通过遍历 branch_list 中的每个 HashMap 来找到特定元素的索引

    public int search_key(String search_key) {
        for (int temp = 0; temp < branch_list.size(); temp++) {
            String id = branch_list.get(temp).get("id");
            if (id != null && id.equals("search_key")) {
                return temp;
            }
        }
        return -1;
    }
    

    使用您要搜索的特定 id 调用上述方法search_key("2572"),如果存在,它将返回 HashMap 的索引,否则返回 -1。

    【讨论】:

      【解决方案3】:

      如果我做对了,您需要在HashMap 中获取键的索引!首先尝试LinkedHashMap 而不是HashMap 以相同的顺序返回键,然后:

      ArrayList keys = new ArrayList(map.keySet());
      for (int i = 0; i < keys.size(); i++) {
          Object obj = keys.get(i);
          System.out.println("Key: "+obj+" at: "+i);
      }
      

      在这里,您将按顺序循环键并将每个键保存在一个对象中,然后尝试将其添加到数组列表中或使用它执行任何您需要的操作

      【讨论】:

        【解决方案4】:

        终于找到了一个简单的解决方案:

         public static int getIndexOFValue(String value) {
            int position=0;
            for (int i = 0; i <Constants.questions_arr_list.size(); i++) {
                HashMap<String, String> map=Constants.questions_arr_list.get(i);
                String id=map.get("id");
                if (id.matches(value)) {
                    position=i;
                    break;
                } 
                
            }
            Log.d("questionIsBranchedOrNot", "true="+position);
            return position;
          
        }
        

        【讨论】:

          猜你喜欢
          • 2021-08-11
          • 1970-01-01
          • 2023-03-24
          • 2016-06-11
          • 2013-04-29
          • 1970-01-01
          • 1970-01-01
          • 2018-12-12
          • 1970-01-01
          相关资源
          最近更新 更多