Using HashMap to map a String and int

前端 未结 1 1390
一向
一向 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:

      
      
          
              USA
              Russia
          
      
          
              1
              7
          
      
      

      And code:

      String[] countriesNames = getResources().getStringArray(R.array.countries_names);
      int[] countriesCodes = getResources().getIntArray(R.array.countries_codes);
      
      HashMap myMap = new HashMap();
      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)
提交回复
热议问题