如何在android中通过选中复选框从列表视图中删除多个条目
我有一个带有复选框的列表视图,可以从外部数据库加载用户的数据。当我选中任何特定复选框并按delete键时,它将删除该特定值。但我的问题是,当我选中多个复选框并按delete键时,它只删除最后一个选中的值,而不是所有选中的值 那么我在代码中做错了什么。谁能推荐一下吗。下面是我的代码。先谢谢你 CustomClassAdapter如何在android中通过选中复选框从列表视图中删除多个条目,android,android-listview,delete-row,android-checkbox,Android,Android Listview,Delete Row,Android Checkbox,我有一个带有复选框的列表视图,可以从外部数据库加载用户的数据。当我选中任何特定复选框并按delete键时,它将删除该特定值。但我的问题是,当我选中多个复选框并按delete键时,它只删除最后一个选中的值,而不是所有选中的值 那么我在代码中做错了什么。谁能推荐一下吗。下面是我的代码。先谢谢你 CustomClassAdapter public class Custom_ClassAdapter extends BaseAdapter { LayoutInflater inflater; Cont
public class Custom_ClassAdapter extends BaseAdapter {
LayoutInflater inflater;
Context context;
List<List_ClassModel> rowItems;
public Custom_ClassAdapter(Context context, List<List_ClassModel> rowItems) {
inflater = LayoutInflater.from(context);
this.context = context;
this.rowItems = rowItems;
}
private class ViewHolder {
ImageView mImageView;
TextView mTextView;
CheckBox mCheckBox;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.custom_class, null);
holder = new ViewHolder();
holder.mImageView = (ImageView) convertView
.findViewById(R.id.classImage);
holder.mTextView = (TextView) convertView
.findViewById(R.id.classname);
holder.mCheckBox = (CheckBox) convertView
.findViewById(R.id.checkBox1);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.mImageView.setImageResource(position);
holder.mCheckBox.setTag(position);
holder.mCheckBox
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
Code.classDel = (Integer) buttonView.getTag();
}
});
List_ClassModel rowItem = (List_ClassModel) getItem(position);
holder.mTextView.setText(rowItem.getClassName());
return convertView;
}
@Override
public int getCount() {
return rowItems.size();
}
@Override
public Object getItem(int position) {
return rowItems.get(position);
}
@Override
public long getItemId(int position) {
return rowItems.indexOf(getItem(position));
}
public void clear() {
}
}
从代码的结构来看,我认为它只能用于删除单个项目 这就是发生的情况:当您选中项目x时,该项目将被存储以供删除。然后,当您选中项目y时,上一个id将被新id的id覆盖。因此,当您向服务器发出请求时,您只有最后一个选中的项目
您可以做的是:创建一个ArrayList。无论何时选中任何项,都将该项添加到ArrayList。因此,您可以存储任意多个要删除的项目。最后,在向服务器发出请求时,遍历ArrayList以获取所有项的ID,并将它们以所需格式发送到服务器。在下面的侦听器中,您将要删除的项存储在Code类中定义的公共静态变量中。任何时候您选中或取消选中(是的,取消选中,因为您没有选中isChecked变量)classDel变量都会被覆盖。因此,您只删除最后一个与之交互的项
holder.mCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
Code.classDel = (Integer) buttonView.getTag();
}
});
您要做的是创建一个数组列表并相应地修改它。下面是一个示例代码:
holder.mCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if(isChecked){ // checked an item, add it to the list
Code.arrayList.add(buttonView.getTag());
} else { // unchecked an item, remove it from the list
Code.arrayList.remove(buttonView.getTag())
}
}
});
在异步任务中,遍历此列表并删除所有项
protected void onPostExecute(String file_url) {
pDialog.dismiss();
for(int item : Code.arrayList)
Code.arrayClasses.remove(item);
mCustom_ClassAdapter.notifyDataSetChanged();
}
如果我使用上面的代码,那么我必须在这里传递的是List\u ClassModel mlList\u ClassModel=code.arrayclass.get(code.classDel);因为若我传递Code.arrayList,那个么它就会显示错误。我不知道你们在代码类中做了什么。您必须相应地更新该类。我刚刚在代码类中将classDel声明为int变量。公共静态int-classDel;您需要将其设置为ArrayList,但如果我这样做,则此语句中会出现错误。List\u ClassModel mlList\u ClassModel=Code.arrayclass.get(Code.arrayList);
holder.mCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if(isChecked){ // checked an item, add it to the list
Code.arrayList.add(buttonView.getTag());
} else { // unchecked an item, remove it from the list
Code.arrayList.remove(buttonView.getTag())
}
}
});
protected void onPostExecute(String file_url) {
pDialog.dismiss();
for(int item : Code.arrayList)
Code.arrayClasses.remove(item);
mCustom_ClassAdapter.notifyDataSetChanged();
}