问题在于在滚动时保存复选框的状态。当我向下或向上滚动时,由于屏幕重绘,一些未选中的项目会被选中。如果有人可以帮忙请。我将提供适配器的代码。
final class ExpAdapter extends CursorTreeAdapter {
private ArrayList<Boolean> itemChecked = new ArrayList<Boolean>();
LayoutInflater mInflator;
CursorTreeAdapter cur;
class ViewHolder {
public TextView textView;
public CheckBox checkbox;
}
public ExpAdapter(Cursor cursor, Context context)
{
super(cursor, context);
mInflator = LayoutInflater.from(context);
}
@Override
protected void bindChildView(View view, Context context, final Cursor cursor,
boolean isLastChild) {
//TextView tvChild = (TextView) view.findViewById(android.R.id.text1);
final TextView tvChild = (TextView) view.findViewById(R.id.label);
tvChild.setText(cursor.getString(cursor
.getColumnIndex(tablename.columname...)));//child
final CheckBox chb = (CheckBox)view.findViewById(R.id.chkbox);
chb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if (buttonView.isChecked()) {
//buttonView.setChecked(true);
buttonView.setChecked(true);
groupcode = mCuror2.getInt(tablename.columname));
levelid = mCuror2.getInt(mCuror2.getColumnIndex(tablename.columname));
levelidarray.add(String.valueOf(levelid));
groupcodearray.add(String.valueOf(groupcode));
String x=(String)tvChild.getText();
array.add(x);
Toast.makeText(getApplicationContext(),""+ array, Toast.LENGTH_SHORT).show();
// do some operations here
}
else
{
buttonView.setChecked(false);
//buttonView.setChecked(false);
//itemChecked.set(cursor.getPosition(), false);
// do some operations here
String x = (String)tvChild.getText();
levelidarray.remove(String.valueOf(levelid));
groupcodearray.remove(String.valueOf(groupcode));
array.remove((String)x);
Toast.makeText(getApplicationContext(),""+ array, Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
protected void bindGroupView(View view, Context context, Cursor cursor, boolean isExpanded)
{
TextView tvGrp = (TextView) view.findViewById(android.R.id.text1);
tvGrp.setText(cursor.getString(cursor
.getColumnIndex(tablename.columname)));
TextView code = (TextView) view.findViewById(android.R.id.text2);
}
@Override
protected Cursor getChildrenCursor(Cursor groupCursor)
{
int groupId = groupCursor.getInt(groupCursor
.getColumnIndex(tablename.columname));
String filter2 = " LEVEL_ID = " +groupId;
mCuror2 = dbconnection....,filter2, null, null, null, null);
return mCuror2;
}
@Override
protected View newChildView(Context context, Cursor cursor,
boolean isLastChild, ViewGroup parent)
{
View mView = mInflator.inflate(R.layout.listviewcheckbox, null);
final TextView tvChild = (TextView) mView.findViewById(R.id.label);
tvChild.setText(cursor.getString(cursor
.getColumnIndex(tablename.columname)));
CheckBox chb = (CheckBox)mView.findViewById(R.id.chkbox);
chb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if (buttonView.isChecked()) {
//buttonView.setChecked(true);
buttonView.setChecked(true);
groupcode = mCuror2.getInt(mCuror2.getColumnIndex(tablename.columname));
levelid = mCuror2.getInt(mCuror2.getColumnIndex(tablename.columname));
levelidarray.add(String.valueOf(levelid));
groupcodearray.add(String.valueOf(groupcode));
String x=(String)tvChild.getText();
array.add(x);
Toast.makeText(getApplicationContext(),""+ array, Toast.LENGTH_SHORT).show();
// do some operations here
}
else
{
buttonView.setChecked(false);
//buttonView.setChecked(false);
//itemChecked.set(cursor.getPosition(), false);
// do some operations here
String x = (String)tvChild.getText();
levelidarray.remove(String.valueOf(levelid));
groupcodearray.remove(String.valueOf(groupcode));
array.remove((String)x);
Toast.makeText(getApplicationContext(),""+ array, Toast.LENGTH_SHORT).show();
}
}
});
return mView;
}
@Override
protected View newGroupView(Context context, Cursor cursor,
boolean isExpanded, ViewGroup parent)
{
View mView = mInflator.inflate(
android.R.layout.simple_expandable_list_item_2, null);
TextView tvGrp = (TextView) mView.findViewById(android.R.id.text1);
tvGrp.setText(cursor.getString(cursor
.getColumnIndex(tablename.columname)));
TextView code = (TextView)mView.findViewById(android.R.id.text2);
code.setText(cursor.getString(cursor.getColumnIndex(tablename.columname)));
return mView;
}