1

I have a simple list view that is attached to an array adapter. My list is a list of multi select checkboxes.

To start I just used android CheckedTextView for my lists text view:

public static class MyAdapter extends ArrayAdapter<String> {
    private List<String> items;

    public MyAdapter(Context context, List<String> items) {
        super(context, android.R.layout.simple_list_item_multiple_choice, items);

        this.items = items;
    }
     ...

This works just great when I click a row the check box gets checked.

My view has gotten more complicated and I had a need to add a progress bar to each item in my list:

list_item.xml

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

    <ProgressBar
        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:indeterminate="true"
        android:visibility="visible" />

    <CheckedTextView
        android:id="@+id/checkedTextView"
        android:layout_width="fill_parent"
        android:layout_height="?android:attr/listPreferredItemHeightSmall"
        android:checkMark="?android:attr/listChoiceIndicatorMultiple"
        android:gravity="center_vertical"
        android:paddingLeft="6dip"
        android:paddingRight="6dip"
        android:text="Rivers"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</LinearLayout>

So my array adapter now looks like:

public static class MyAdapter extends ArrayAdapter<String> {
    private List<String> items;

    public MyAdapter(Context context, List<String> items) {
        super(context, R.layout.list_item, R.id.checkedTextView, items);

        this.items = items;
    }

With that simple change to use my own layout "R.layout.list_item", when I click a row its checkbox does not get checked. What magic was happening before that made this work when I used androids "android.R.layout.simple_list_item_multiple_choice" layout?

I.E. somewhere android handled the click event and checked the box, but all I did is change the layout, and now it doesn't work. I imagine I got something for free by using there layout. Can I setup my layout differently to still get the free check on click?

lostintranslation
  • 23,756
  • 50
  • 159
  • 262
  • check this http://stackoverflow.com/questions/2627222/android-checkedtextview-cannot-be-checked – Libin Apr 10 '14 at 23:38
  • User of question was using it outside a listview. I am using it inside a listview. Seems like a lot of those comments refer to the listview handling the checked state. That is exactly what I want. Why when I change the layout of my list item is my list view not handling the check state? – lostintranslation Apr 11 '14 at 00:57

1 Answers1

1

The magic is that CheckedTextView implements Checkable. If you want your own list item view that will work with all the click/check events you need your view to implement that.

Answer w/ example found here: http://tokudu.com/post/50023900640/android-checkable-linear-layout

lostintranslation
  • 23,756
  • 50
  • 159
  • 262