0

I use a listview with multiple choice and I override the search method inside. I have a problem with the items in that during scrolling the position of items changed. The problem comes from the layout, I think, because when the layout includes only the listview, it works correctly. But, when I use the layout below, the positions of the items checked during scrolling get changed.

Can anyone help with that please?

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent">

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_toLeftOf="@+id/button1" />


    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="Button" />

    <ListView
        android:id="@+id/MyListView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/editText1" />

</RelativeLayout>
Dipak Keshariya
  • 22,193
  • 18
  • 76
  • 128
steevoo
  • 621
  • 6
  • 18
  • That really sounds like a view recycling issue in the list, but it should be present regardless of whether or not the list is by itself or in with other widgets. Are you certain it doesn't happen when the list is the only thing in the layout? – Barak May 30 '12 at 13:05
  • i notice now in the 2 state the position changed before was not anyhow how can i resolve this issue – steevoo May 30 '12 at 13:40
  • see this link : http://stackoverflow.com/q/10481066/1168654 – Dhaval Parmar May 30 '12 at 14:11

1 Answers1

0

You need to set up an object in your adapter to keep track of the state of the checkboxes. You haven't shown your adapter code so I'll just pull an example from some of my code (this is using a cursor, it can be adapted to an ArrayAdapter)

In the custom adapter set an array:

private Cursor c; 
public static int[] checked; 

In your constructor set your cursor to the local var and call a method:

CursorAdapter_EditText.c = c; 
initializeChecked(); 

The method (using 0 = not checked):

public static void initializeChecked() { 
    checked = new int[c.getCount()]; 
    int i = 0; 
    while (i < c.getCount()) { 
        checked[i] = 0; 
        i++; 
    } 
} 

In your getView use the array to set the checkbox state:

checkBox = (CheckBox) view.findViewById(R.id.CheckBox); 
if (checked(position) == 0) { 
    checkBox.setChecked(false); 
} else { 
    checkBox.setChecked(true); 
}  

You'll need to add code in checkBox.setOnCheckedChangeListener also to change the state in the array when you change the state of the checkbox.

Barak
  • 16,318
  • 9
  • 52
  • 84