当我点击 ListView 中的复选框时,列表中的第二项也会被选中。例如,如果我选中第一项,则列表中的第 7 项将被选中。这是我的单元格布局代码:
<?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>
我加入了一张图片来帮助你:
附带说明一下,ListView 位于 ListFragment 内。
编辑:下面是将我的单元格与其数据映射的代码。
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);
编辑#2:如果 ListView 垂直显示,则不会发生这种情况。
编辑#3:即使我使用 SimpleAdapter,使用以下代码,当 ListView 是水平的时,我也会遇到同样的错误。
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;
}
}
编辑#5:这是解决方案!覆盖 getViewTypeCount() 和 getItemViewType(int position) 方法。我已将这些行添加到 SimpleAdapter 并且它可以工作。
@Override
public int getViewTypeCount() {
return getCount();
}
@Override
public int getItemViewType(int position) {
return position;
}