2012-04-26 65 views
0

这是我的java列表。如何将1个以上的值放入列表中?

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> 
+0

您可以发布newscommentlist xml文件? – 2012-04-26 11:12:43

+0

它看起来像您的评论,commentdate,commentposter具有相同的值。您不能在HashMap上使用相同的值。 – digulino 2012-04-26 11:14:11

+0

每次添加相同的地图 – 2012-04-26 11:14:49

回答

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

有什么区别? – 2012-04-27 02:34:37

+0

是的。我用双引号将所有值写入。参见字符串数组。 – 2012-04-27 02:55:59

+0

哦,我明白了 – 2012-04-27 03:23:35