如何获取哈希映射数组列表的索引,这是我的代码
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 = Page 1 Question 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 = Page 1问题02,其他= 0,page_yes = 0,branching_status = 0,page_no = 0,required = 0,survey_id = 592,type_id = 9,page_dontno = 0}, {id = 2572,title = Page 1问题03分支问题,其他= 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问题02分支问题,其他= 0,page_yes = 5,branching_status = 1,page_no = 0,required = 0,survey_id = 592,type_id = 9,page_dontno = 0}]
答案 0 :(得分:0)
如果我理解正确,您想将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
答案 1 :(得分:0)
您可以通过遍历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。
答案 2 :(得分:0)
如果我做对了,你需要在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);
}
在这里,您将按顺序循环键并将每个键保存在一个对象中,然后尝试将其添加到数组列表中或使用它执行任何操作
答案 3 :(得分:0)
最后,我自己找到了简单的解决方案,它可能对某个人有所帮助。
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;
}