SimpleAdapter显示一个空列表

时间:2014-04-11 19:53:28

标签: android listview simpleadapter

代码编译没有错误,但结果只是我手机上的空列表。那么问题是什么?这是我在Activity中的onCreate方法

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    TextView textView;
    ArrayList<HashMap<String, String>> list=new ArrayList<HashMap<String, String>>();
    HashMap<String, String> m;
    for(int i=0; i<=10; i++) {
        m=new HashMap<String, String>();
        m.put("key", "value"+i);
        list.add(m);
    }
    String[] from={"key"};
    int[] to={R.id.text1};
    ListView lv=(ListView)findViewById(R.id.listView);
    SimpleAdapter sa=new SimpleAdapter(this, list, R.layout.row, from, to);        
    lv.setAdapter(sa);

main.xml中:

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

    <ListView xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_height="fill_parent"
            android:layout_width="fill_parent"
            android:id="@+id/listView"
    />
</LinearLayout>

row.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
    <TextView android:layout_width="0px" android:layout_height="fill_parent"
              android:id="@+id/text1"
              android:text="dddddddd"
            />    
</LinearLayout>

1 个答案:

答案 0 :(得分:1)

row.xml中,更改

<TextView android:layout_width="0px" android:layout_height="fill_parent"

<TextView android:layout_width="wrap_content" android:layout_height="fill_parent"

0px的宽度会使视图不可见,并且只能与layout_weight一起使用