Tihamer
Tihamer

Reputation: 73

SimpleAdapter showing an empty list

The code is compile without errors, but the result is just an empty list on my phone. So what is the problem? Here is my onCreate method in Activity

    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>

Upvotes: 2

Views: 398

Answers (1)

Ken Wolf
Ken Wolf

Reputation: 23269

In row.xml, change

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

to

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

A width of 0px will make the view invisible and should only be used in conjunction with layout_weight

Upvotes: 1

Related Questions