HashMap values not being appended to ListView

后端 未结 1 449
梦毁少年i
梦毁少年i 2021-01-27 18:31

i\'m trying to retrieve data from a hashmap with multiple values for 1 key and set it to a listview,but instead of setting the values into the listview and displaying the listv

相关标签:
1条回答
  • 2021-01-27 19:09

    SimpleAdapters Consturctor states as it's second parameter:

    data: A List of Maps. Each entry in the List corresponds to one row in the list. The Maps contain the data for each row, and should include all the entries specified in "from"

    but HashMap<String, List<String>> hm is a map of lists. So like List<Map<String,String>> hm would be the datatype you probably need.

    Here is the edited source:

     ListView lv = (ListView)findViewById(R.id.list);
                List<Map<String,String>> mapList = new ArrayList<Map<String, String>>();
                Map<String,String> mapPerRow;
                for (int i = 0; i < rowNumbers; i++) {
                    mapPerRow = new HashMap<String, String>();
                    mapPerRow.put("column1", value1);
                    mapPerRow.put("column2", value2);
    
                    mapList.add(mapPerRow);
                }
    
    
                ListAdapter adapter = new SimpleAdapter(
                        MainActivitty.this,  mapList,
                        R.layout.list_item, new String[] { "column1", "colum2"},
                        new int[] { R.id.value1,R.id.value2 });
                // updating listview
                lv.setAdapter(adapter);
    

    I don't get why you want the key in it (just add Strings to the map if you need more)?

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