本文介绍了HashMap的值不被追加到的ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要检索与多个值HashMap的数据,1项并将其设置为一个列表视图,但是设置,而不是值进入列表视图,并显示在列表视图,所有这一切都显示是数组(无钥匙)。在code是如下:

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 valuesinto the listview and displaying the listview,all that is displayed is the array(without the key).The code is as follows:

ListView lv = (ListView)findViewById(R.id.list);
    //hashmap of type  `HashMap<String, List<String>>`
    HashMap<String, List<String>> hm = new HashMap<String, List<String>>();
    List<String> values = new ArrayList<String>();
    for (int i = 0; i < j; i++) {
        values.add(value1);
        values.add(value2);
        hm.put(key, values);
    }

和检索值,并把在ListView

and to retrieve the values and put in a listview

ListAdapter adapter = new SimpleAdapter(
                        MainActivitty.this,  Arrays.asList(hm),
                        R.layout.list_item, new String[] { key,
                                value1,value2},
                        new int[] { R.id.id, R.id.value1,R.id.value2 });
                // updating listview
                lv.setAdapter(adapter);

一个例子是,其中的关键= 1,值2 = 2和值3 = 3,它会显示一个数组,看起来像[2,3]。我如何得到它显示lisview并添加关键吗?

an example is where the key=1,value2=2 and value3=3,it will display the an array that looks like [2,3].how do i get it to display the lisview and add the key too?

推荐答案

SimpleAdapters Consturctor状态,因为它的第二个参数:

SimpleAdapters Consturctor states as it's second parameter:

数据:地图列表。在列表中的每个条目对应于一个行中的  列表。所述地图包含每个行的数据,并应包括  所有,从中指定的条目

的HashMap&LT;字符串列表与LT;字符串&GT;&GT; HM是列表的图。因此,像名单,其中,地图&LT;字符串,字符串&GT;&GT; HM 将是你可能需要的数据类型。

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.

下面是经过编辑的源:

 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)?

这篇关于HashMap的值不被追加到的ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 09:24