如何获取Hashmap数据

时间:2016-09-01 05:01:24

标签: android hashmap android-recyclerview

我见过很多关于Hashmap数据的例子,但我没有按要求获取数据。

这是我的代码:

HashMap<String,ArrayList<String>> citylist = new HashMap<String, ArrayList<String>>();

    ArrayList<String> Gujarat = new ArrayList<String>();
    Gujarat.add("Surat");
    Gujarat.add("Baroda");
    Gujarat.add("Ahmedabad");

    ArrayList<String> Rajasthan = new ArrayList<String>();
    Rajasthan.add("Udaipur");
    Rajasthan.add("Jaipur");

    ArrayList<String> UP= new ArrayList<String>();
    UP.add("Lucknow");
    UP.add("Agra");

    citylist.put("Gujarat", Gujarat);
    citylist.put("UP", UP);
    citylist.put("Rajasthan", Rajasthan);

在recyclelerview中如何在BindViewHolder中获取此类数据? 吐司就像:

   {Rajasthan=[Udaipur, Jaipur], UP=[Lucknow, Agra], Gujarat=[Surat, Baroda, Ahmedabad]}

我曾使用过这种方法但错误即将来临:

    public void onBindViewHolder(MyViewHolder holder, int position) {
    ArrayList<String> lst = citylist.get("" + position);
    for (Integer i = 0; i < lst.size(); i++) {
        holder.tv.setText(citylist.toString());
        Log.e("Hashmap....", ""+holder.tv );
    }

输出应该像古吉拉特邦是州和苏拉特巴罗达和艾哈迈达巴德是城市?

6 个答案:

答案 0 :(得分:1)

首先使用所有州创建一个ArrayList

ArrayList<String> stateList = new ArrayList<String>();
stateList.add("Gujarat");
stateList.add("UP");
stateList.add("Rajasthan");

其次创建一个HashMap,每个州名称为Key,每个州城市为Value

HashMap<String,ArrayList<String>> stateCityMap = new HashMap<String,ArrayList<String>>()

ArrayList<String> gujaratCityList = new ArrayList<String>();
gujaratCityList.add("Ahmedabad");
gujaratCityList.add("Surat");
gujaratCityList.add("Baroda");
.......................

ArrayList<String> upCityList = new ArrayList<String>();
upCityList.add("Lucknow");
upCityList.add("Agra");
..........................

ArrayList<String> rajasthanCityList = new ArrayList<String>();
rajasthanCityList.add("Udaipur");
rajasthanCityList.add("Jaipur");
...........................

stateCityMap.put("Gujarat",gujaratCityList);
stateCityMap.put("UP",upCityList);
stateCityMap.put("Rajasthan",rajasthanCityList);

现在根据Adapter中的状态获取所有城市名称:

public void onBindViewHolder(MyViewHolder holder, int position) {
   Log.e("State : ",stateList.get(position));
   ArrayList<String> cityList=   (ArrayList<String>)stateCityMap.get(stateList.get(position));
   for(String cityName : cityList){
      Log.e("City : ",cityName);
   }
}

答案 1 :(得分:0)

你可以如下所示。

Iterator iterator = map.keySet().iterator();
while(iterator.hasNext()) {
    String key=(String)iterator.next();
    String value=(String)map.get(key);
    Toast.makeText(ctx, "Key: "+key+" Value: "+value, Toast.LENGTH_LONG).show();
}

答案 2 :(得分:0)

HashMaps不会保留排序:

  

此课程不保证地图的顺序;在   特别是,它不保证订单将保持不变   随着时间的推移。

查看LinkedHashMap,它保证了可预测的迭代顺序。

答案 3 :(得分:0)

你可能想尝试这样的事情。

for (Map.Entry<String, ArrayList<String>> entry : citylist.entrySet()) 
{
    String key = entry.getKey();        // Your State
    String value = entry.getValue();    // Your List of Cities.

    // Split data and insert in Views
}

但我建议(为了便于使用案例)保留所有州的List,并使用此Value州的密钥从HashMap获取List。< / p>

答案 4 :(得分:0)

请检查:

    ArrayList<HashMap<String, String>> arrayList = new ArrayList<HashMap<String,String>>();
    HashMap<String, String> h1 = new HashMap<String, String>();
    h1.put("h1_key_1", "h1_value_1");
    h1.put("h1_key_2", "h1_value_2");
    arrayList.add(h1);

    for (HashMap<String, String> hashMap : arrayList) {
    System.out.println(hashMap.keySet());
    for (String key : hashMap.keySet()) {
    System.out.println(hashMap.get(key));
    }
}

答案 5 :(得分:0)

  Try This:

    public static void printMap(Map mp) {
Iterator it = mp.entrySet().iterator();
while (it.hasNext()) {
    Map.Entry pair = (Map.Entry)it.next();
    System.out.println(pair.getKey() + " = " + pair.getValue());
    it.remove(); // avoids a ConcurrentModificationException
}
}