【问题标题】:Setting values from a HashMap with multiple values for 1 key in a listview使用列表视图中 1 个键的多个值从 HashMap 设置值
【发布时间】:2013-06-22 20:14:30
【问题描述】:

我创建了以下 HashMap<String, List<String>> 类型的 HashMap,并正在为其添加值,如下所示:

ListView lv = (ListView)findViewById(R.id.list);

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);
}

我想检索数据,将其解析为 ListAdapter,然后解析为 ListView。这是列表视图的 xml:

<ListView
    android:id="@+id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"/>

以及列表项的 xml:

 <!-- for the key value -->
 <TextView
        android:id="@+id/id"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
         />


    <!-- For Value 1 -->
    <TextView
        android:id="@+id/value1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
         />

      <!-- For Value 2 -->
    <TextView
        android:id="@+id/name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
         />

如何从 hashMap 中检索数据,将其放入 ListAdapter,然后将其设置到 ListView?

【问题讨论】:

    标签: java android hashmap


    【解决方案1】:

    您可以使用以下代码

    ListAdapter adapter = new SimpleAdapter(this, hm , R.layout.listplaceholder, new String[] { "value1",
                    "value2" }, new int[] { R.id.value1, R.id.name});
    list.setAdapter(adapter);
    

    listplaceholder 是我的 xml。所以请改变它。

    你也可以关注这个tutorial

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-28
    • 2012-05-17
    • 1970-01-01
    • 1970-01-01
    • 2015-06-28
    • 2017-10-11
    相关资源
    最近更新 更多