Using HashMap to map a String and int

前端 未结 1 1359
一向
一向 2021-02-02 02:19

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,

相关标签:
1条回答
  • 2021-02-02 02:42
    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

    0 讨论(0)
提交回复
热议问题