2

I have a dialog that contains listview with checkboxes and a button that becomes visible if at least one checkbox is checked. Click on the button removes checked lines from the listview. But in this dialog I have a strange behaviour of the checkboxes in the list. For instance, if the list contains four lines, and I check one of them no more items can be checked anymore. If I add more items into the list then I can check more than one, but if I remove some items, remaining items become uncheckable.

PS. Is there a way to fix or workaround it anyhow?

UPDATE1: I found out that checkboxes do not become absolutelly inactive and if to try to click one several times it can become checked. It looks like area around the check box that is recognized as onClick area is set too little.

UPDATE2: Also I found out that if I comment this line from the dialog initialization code

getWindow ().setLayout (LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

then all the checkboxes in the list become uncheckable right after dialog instantiation.

Dialog layout

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:minHeight="200dip"
  android:orientation="vertical"
  android:background="@android:color/white"
>
  <ListView
    android:id="@+id/list_view"
    android:layout_width="fill_parent"
    android:layout_height="0dip"
    android:layout_weight="1"
    android:divider="@color/list_divider"
    android:dividerHeight="2px"
    android:focusable="false"
    android:clickable="false"
    android:choiceMode="multipleChoice"
    />
  <LinearLayout
    android:id="@+id/buttons_pane"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:visibility="gone"
  >
    <Button
      android:id="@+id/misc_list_remove_btn"
      android:layout_width="fill_parent"
      android:layout_height="48dip"
      android:text="@string/remove"
    />
  </LinearLayout>
</LinearLayout>

Item layout

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="47dip"
  android:orientation="horizontal"
  android:background="@android:color/white"
>
  <CheckBox
    android:id="@+id/misc_selectable_list_item_chk"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:checked="false"
  />
</LinearLayout>

I create the dialog this way

public ShowSelectableListDialog (Activity context, int titleId)
{
    super (context);
    setContentView (R.layout.misc_selectable_list_dlg);
    setTitle (titleId);
    getWindow ().setLayout (LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    adapter_ = setupListAdapter (getInitialValues (), context);
    ListView listView = (ListView)findViewById (R.id.list_view);
    listView.setAdapter (adapter_);
    listView.setItemsCanFocus (false);
    Button removeButton = (Button)findViewById (R.id.misc_list_remove_btn);
    removeButton.setOnClickListener (this);
  }

and item in the list

@Override
protected View instantiateView (Object ... params)
{
  String title = (String)params[0];
  CompoundButton.OnCheckedChangeListener checkedChangeListener =
    (CompoundButton.OnCheckedChangeListener)params[1];

  LayoutInflater inflater = Utils.getInflater (getContext ());
  View view = inflater.inflate (R.layout.misc_selectable_list_item, null);
  view.setId (title.hashCode ());
  CheckBox chk = (CheckBox)view.findViewById (R.id.misc_selectable_list_item_chk);
  chk.setText (title);
  chk.setOnCheckedChangeListener (checkedChangeListener);
  return view;
}
Artemy Kilin
  • 176
  • 15
  • I think the problem is in your ClickListener. Try disabling it (i.e. comment out the line `chk.setOnChangeListener()`. – bos Nov 11 '11 at 06:55
  • If I remove the click listener and comment out the show/hide button code, then I can choose more than one item, but after I removed checked items from my list I cannot choose anymore. – Artemy Kilin Nov 11 '11 at 07:19
  • Checkout this also http://stackoverflow.com/questions/7738527/getting-an-issue-while-checking-the-dynamically-generated-checkbox-through-list/7738854#7738854 – Lalit Poptani Nov 15 '11 at 06:07

1 Answers1

0

The problem appeared because I cached newly created views within item of my list adapter. It works fine (at least without visible problems) if list items do not contain controls like checkboxes or buttons, but in some cases can become a reason of problems.

Correct way to avoid list item views reintstantiation is to use convertView parameter of the getView method, declared in android.widget.Adapter.

Case closed.

Artemy Kilin
  • 176
  • 15