When I tap a checkbox inside a ListView, a second item in the list becomes also checked. As example, if I check the 1st item, the 7th in the list becomes checked. Here is my code for the cell’s layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/relativeLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/imgDisclosureIndicator"
android:src="@drawable/disclosureon"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:paddingRight="6dp"></ImageView>
<ImageView
android:id="@+id/imgTaskState"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/taskok" />
<CheckBox
android:id="@+id/chkTaskImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/imgTaskState"
android:layout_toLeftOf="@+id/imgDisclosureIndicator"
android:layout_toRightOf="@+id/imgTaskState"
android:focusable="false" />
</RelativeLayout>
I joined an image to help you out:

As a side note, the ListView is inside a ListFragment.
EDIT: Below is the code that maps my cell with its data.
String[] from = {"taskId", "taskStateId", "taskText", "disclosureIndicatorId"};
int[] to = {0, R.id.imgTaskState, R.id.chkTaskImage, R.id.imgDisclosureIndicator};
SimpleAdapter adapter = new SimpleAdapter(mContext, mList, R.layout.task_cell, from, to);
setListAdapter(adapter);
EDIT #2: This situation doesn’t happens if the ListView is shown vertically.

EDIT #3 : Even if I’m using a SimpleAdapter, with the following code, when the ListView is horizontal I’m getting the same error.
private class CellInspectionAdapter extends BaseAdapter
{
private Vector<InspectionCell> cells;
private Context context;
public CellInspectionAdapter(Context context, Vector<InspectionCell> cells)
{
this.cells = cells;
this.context = context;
}
@Override
public int getCount()
{
return this.cells.size();
}
@Override
public Object getItem(int position)
{
return null;
}
@Override
public long getItemId(int position)
{
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
// Map data from the ArrayList from the adapter
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View celluleTask;
if (convertView == null)
{
celluleTask = new View(context);
celluleTask = inflater.inflate(R.layout.laprise_task_cell, null);
// Assign data to the cell
ImageView imgEtat = (ImageView)celluleTask.findViewById(R.id.imgTaskState);
imgEtat.setImageResource(cells.get(position).getStateDrawable());
// Assign text
CheckBox chkCell = (CheckBox)celluleTask.findViewById(R.id.chkTaskImage);
chkCell.setText(cells.get(position).getTexte());
// Disclosure
ImageView imgDisclosure = (ImageView)celluleTask.findViewById(R.id.imgDisclosureIndicator);
imgDisclosure.setImageResource(cells.get(position).getDisclosureDrawable());
}
else
{
celluleTask = (View)convertView;
}
return celluleTask;
}
}
EDIT #5: Here is the solution! Override the getViewTypeCount() and getItemViewType(int position) methods. I’ve added these lines to the SimpleAdapter and it works.
@Override
public int getViewTypeCount() {
return getCount();
}
@Override
public int getItemViewType(int position) {
return position;
}
You’re using
convertViewincorrectly. A non-nullconvertViewonly spares you the creation of a new View and doing the layout stuff, but you have to set the contents of all the children of theconvertViewto match the current item. That also explains why it works in vertical because then both rows are visible simultanously.Try this:
P.S.: You also should move the fetching of the layout inflater out of the
getViewmethod so it has not to be done over and over again.P.P.S.: To avoid calling
findViewByIdagain and again, have a look at the View Holder Pattern.