即使适配器不为空,ListView也不显示数据

时间:2014-08-23 08:02:17

标签: android android-listview

我正在尝试使用ListView显示一些键值对,但即使adapter不是null,也不显示数据。 < / p>

Checkout.java: -

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.checkout);

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

ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
map.put("shopK", "SHOP NAME");
map.put("shopV", shop);
mylist.add(map);
map = new HashMap<String, String>();
map.put("subjectK", "SUBJECT");
map.put("subjectV", subject);
mylist.add(map);
for(int i=0;i<products.size();i++){
    map = new HashMap<String, String>();
    map.put("productK", "PRODUCT NAME");
    map.put("productV", products.get(i));
    mylist.add(map);    
}
SimpleAdapter checklist = new SimpleAdapter(this, mylist, R.layout.row,
            new String[] {"TITLE", "VALUE"}, new int[] {R.id.TITLE_CELL, R.id.VALUE_CELL});
list.setAdapter(checklist);
}

checkout.xml: -

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

  <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button1"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1" >

    </ListView>

</RelativeLayout>

row.xml: -

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:paddingTop="4dip"
     android:paddingBottom="6dip"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:orientation="horizontal">

     <TextView android:id="@+id/TITLE_CELL"
         android:layout_width="50dip"
         android:layout_height="wrap_content"/>

     <TextView android:id="@+id/VALUE_CELL"
         android:layout_width="70dip"
         android:layout_height="wrap_content" android:layout_weight="1"/>
</LinearLayout>

1 个答案:

答案 0 :(得分:1)

在以下行中更改您的代码:

HashMap<String, String> map = new HashMap<String, String>();
map.put("TITLE", "ShopK");
map.put("VALUE", "SubjectK");
mylist.add(map);
map = new HashMap<String, String>();
map.put("TITLE", "ShopV");
map.put("VALUE", "SubjectV");
mylist.add(map);

SimpleAdapter checklist = new SimpleAdapter(this, mylist, R.layout.row,
            new String[] {"TITLE", "VALUE"}, new int[] {R.id.TITLE_CELL, R.id.VALUE_CELL});
相关问题