n.st
n.st

Reputation: 946

Android ListView with a checkbox and multiple TextViews per list item

I'm quite new to programming Android apps and have now run into a problem for which I couldn't find an explanation (nor an easy solution):

I want to display a list of files containing a checkbox, the filename and filesize. The Android developer docs contain a tutorial on how to implement a ListView and I also managed to change the ListItem-layout to contain the checkbox and two TextViews.

The list looks just how it should, but when I check a checkbox, every seventeenth or so checkbox is checked, too. It looks like the ListView stores the values of the checkboxes page-wise, so that when the values are changed on one page, they change on every page.

Looking for a workaround, I discovered that one could use android.R.layout.simple_list_item_multiple_choice instead of a custom layout - which indeed works fine, but contains only one TextView and is therefore useless for me. Some sites also suggest creating a custom type of TextView (consisting of a LinearLayout, the CheckBox and the TextViews needed) for this, but I would like to avoid having to an extra class just for the ListView to work properly.

I would be glad if anyone could tell me where my error is or whether there is a workaround for this.

My current HelloListViewActivity.java:

public class HelloListViewActivity extends ListActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

      setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, R.id.container, COUNTRIES));

      ListView lv = getListView();
      lv.setTextFilterEnabled(true);

      lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {
          // When clicked, show a toast with the TextView text
          Toast.makeText(getApplicationContext(), ((TextView) (((LinearLayout) view).findViewById(R.id.container))).getText(),
              Toast.LENGTH_SHORT).show();
        }
      });
    }

    static final String[] COUNTRIES = new String[] {
        "Afghanistan", "Albania", "Algeria", "American Samoa", "Andorra"
                    /* etc. */
        };
}

And list_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1">
    <CheckBox 
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:focusable="false"
        android:textSize="16sp" />
    <TextView
        android:gravity="center_horizontal"
        android:layout_weight="1"
        android:text="Text"
        android:layout_width="0dip"
        android:layout_height="fill_parent"
        android:padding="10dp"
        android:textSize="16sp"
        android:id="@+id/container" />
    <TextView
        android:text="---"
        android:gravity="center_horizontal"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="1"/>
</LinearLayout>

Upvotes: 2

Views: 4216

Answers (1)

Ashterothi
Ashterothi

Reputation: 3282

I would create a custom Adapter that extends ArrayAdapter, from there you can override getView() and load the view with the information you want. For a ton of really good information about list views, watch the Google I/O presentation The World of List View

Upvotes: 3

Related Questions