如何将超过1个值放入列表?

时间:2012-04-26 11:08:14

标签: android list

这是我的java列表。

private void filldata() {
    ListView lv = (ListView) findViewById(android.R.id.list);

    String[] from = new String[] { comment, commentdate, commentposter };
    int[] to = new int[] { R.id.text_comment, R.id.text_commentdate,
            R.id.text_commentposter };
    List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
    for (int i = 0; i < 5; i++) {
        HashMap<String, String> map = new HashMap<String, String>();
        map.put(comment, "testing 1");
        map.put(commentdate, "testing 2");
        map.put(commentposter, "testing 3");
        fillMaps.add(map);
    }
    SimpleAdapter adapter = new SimpleAdapter(this, fillMaps,
            R.layout.newscommentlist, from, to);
    lv.setAdapter(adapter);
}

给我的输出是

测试3

测试3

测试3

但我想要的是

测试1

测试2

测试3

如何实现我的期望?

更新:

这是newscommentlist.xml

?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#ffffff" >

<TextView
    android:id="@+id/text_commentdate"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginRight="10px"
    android:layout_marginTop="10px"
    android:gravity="right"
    android:textColor="#ff0000"
    android:textSize="20px" />

<TextView
    android:id="@+id/text_comment"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10px"
    android:layout_marginRight="10px"
    android:layout_marginTop="10px"
    android:gravity="fill_horizontal"
    android:textColor="#000000"
    android:textSize="30px" />

<TextView
    android:id="@+id/text_commentposter"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10px"
    android:layout_marginRight="10px"
    android:layout_marginTop="10px"
    android:gravity="right"
    android:textColor="#000000"
    android:textSize="35px" />

这是newscomments.xml

<ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10px" >
    </ListView>

1 个答案:

答案 0 :(得分:0)

private void filldata() {
    ListView lv = (ListView) findViewById(android.R.id.list);

    String[] from = new String[] { "comment", "commentdate", "commentposter" };
    int[] to = new int[] { R.id.text_comment, R.id.text_commentdate,
            R.id.text_commentposter };
    List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
    for (int i = 0; i < 5; i++) {
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("comment", "testing 1");
        map.put("commentdate", "testing 2");
        map.put("commentposter", "testing 3");
        fillMaps.add(map);
    }
    SimpleAdapter adapter = new SimpleAdapter(this, fillMaps,
            R.layout.newscommentlist, from, to);
    lv.setAdapter(adapter);
}
相关问题