Using HashMap to map a String and int

47,147
  1. As one of the possibilities, you may store 2 different arrays in XML: string array and integer array, and then programmatically put them in the HashMap.

    Definition of arrays:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <string-array name="countries_names">
            <item>USA</item>
            <item>Russia</item>
        </string-array>
    
        <integer-array name="countries_codes">
            <item>1</item>
            <item>7</item>
        </integer-array>
    </resources>
    

    And code:

    String[] countriesNames = getResources().getStringArray(R.array.countries_names);
    int[] countriesCodes = getResources().getIntArray(R.array.countries_codes);
    
    HashMap<String, Integer> myMap = new HashMap<String, Integer>();
    for (int i = 0; i < countriesNames.length; i++) {
        myMap.put(countriesNames[i], countriesCodes[i]);
    }
    
  2. It may be a file with any name. See this

Share:
47,147
kiki
Author by

kiki

android beginner

Updated on October 25, 2020

Comments

  • kiki
    kiki over 3 years

    I have a ListView showing names of countries. I have stored the names in strings.xml as a string-array called country_names.

    In populating the ListView, I use an ArrayAdapter which reads from strings.xml:

    String[] countryNames = getResources().getStringArray(R.array.country_names);
    ArrayAdapter<String> countryAdapter = new ArrayAdapter<String>(this, R.layout.checked_list, countryNames);
    myList.setAdapter(countryAdapter);
    

    Now I also have a CountryCode for each country. When a particular country name is clicked on the ListView, I need to Toast the corresponding CountryCode.

    I understand implementing a HashMap is the best technique for this. As far as I know, the HashMap is populated using put() function.

    myMap.put("Country",28);
    

    Now my questions are:

    1. Is it possible to read the string.xml array and use it to populate the Map? I mean, I want to add items to the Map, but I must be able to do so by reading the items from another array. How can I do this?

      The basic reason I ask is because I want to keep the country names and codes in a place where it is easier to add/remove/modify them.

    2. The string-arrays are stored in strings.xml. Where must similar integer arrays be stored? In values folder, but under any specific XML file?

  • kiki
    kiki over 13 years
    Hi, Sergey. I want to know HOW to programmatically put the values into the HashMap.Could you share a code snippet in your answer. As to 2), thanks, that is what I was looking for.
  • kiki
    kiki over 13 years
    Hey, thanks a lot! I was confused whether put() would work that way.
  • kirhgoff
    kirhgoff over 11 years
    Thats surely a solution, but the data is split to two independant data sets, which could cause a problem, if you are editing these two lists a lot. Is there a way to create as a one resource piece, like maps in Spring, for example?
  • Sergey Glotov
    Sergey Glotov over 11 years
    @kirhgoff I don't know such a way
  • kirhgoff
    kirhgoff over 11 years
    Also didnt find anything, using delimiter in the string and parse the array on the load.