How to get the index of the hash map array list in android?

2k views Asked by At

How to get the index of the hash map array list,here is my code

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 {

    } 

Below is my array list after getting values.I want to get index of id=2572, like answer=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 Question 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=Page 1 Question 03 Branching question, 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}]

4

There are 4 answers

0
Asif Sb On

Finally I found a simple solution:

 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;
  
}
0
Luke Taylor On

If I understand correctly, you want to link an ID of the Hashmap to the index of an ArrayList?

What you could do is create a new HashMap and the key as the ID and the value as the index.

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

Hope this helps

Note: Your would also have to change you existing HashMap to a LinkedHashMap since the order is crucial

0
Kartheek On

You can find the index of the particular element by iterating through each HashMap in the branch_list

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

Call the above method with the specific id you want to search search_key("2572") and it will return the index of the HashMap if present otherwise returns -1.

0
A.S On

if i get it right, you need to get the index of a key in a HashMap! first try LinkedHashMap instead of HashMap to return keys in same order, then:

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

here you will loop around the keys in order and save each key in an object then try to add it to an array list or do what ever you need with it