使用android中的自定义arrayadapter在listview中使用复选框检查后如何删除多个项目?
Posted
技术标签:
【中文标题】使用android中的自定义arrayadapter在listview中使用复选框检查后如何删除多个项目?【英文标题】:How to remove multiple items after checked using checkbox in listview using custom arrayadapter in android? 【发布时间】:2015-06-26 18:02:59 【问题描述】:我有带有 1 个复选框和 2 个标签的 Listview。我为 Listview 使用了自定义 ArrayAdapter 类。我使用选中的复选框成功地从列表视图中删除了项目,但问题是没有正确删除多个项目。
public class MainActivity extends Activity
ListView listView;
Button btnDelete;
ArrayList<String> items = new ArrayList<String>();
ArrayList<Integer> ids = new ArrayList<Integer>();
CustomAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listView1);
btnDelete = (Button) findViewById(R.id.btnDelete);
for (int i = 0; i < 20; i++)
items.add("Chk " + i);
adapter = new CustomAdapter(MainActivity.this, R.layout.custome_list,
items);
listView.setAdapter(adapter);
btnDelete.setOnClickListener(new OnClickListener()
@Override
public void onClick(View v)
if (ids.size() > 0)
for (int i = 0; i < ids.size(); i++)
items.remove(items.get(ids.get(i)));
adapter.notifyDataSetChanged();
);
public class CustomAdapter extends ArrayAdapter<String>
List<String> items;
int resource;
public CustomAdapter(Context context, int resource, List<String> items)
super(context, resource, items);
this.items = items;
this.resource = resource;
@Override
public View getView(final int position, View convertView, ViewGroup parent)
final int pos = position;
ViewHolder holder = null;
if (convertView == null)
holder = new ViewHolder();
LayoutInflater inflater = getLayoutInflater();
convertView = inflater.inflate(resource, null);
holder.chk = (CheckBox) convertView.findViewById(R.id.chk);
holder.txt = (TextView) convertView.findViewById(R.id.txt);
convertView.setTag(holder);
else
holder = (ViewHolder) convertView.getTag();
holder.txt.setText(items.get(position));
holder.chk
.setOnCheckedChangeListener(new OnCheckedChangeListener()
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked)
if (isChecked)
ids.add(position);
System.out.println("IDS A: " + ids.toString());
else
if (ids.contains(position))
int i = ids.indexOf(position);
ids.remove(i);
System.out.println("IDS R: "
+ ids.toString());
);
return convertView;
public class ViewHolder
CheckBox chk;
TextView txt;
【问题讨论】:
androidbegin.com/tutorial/… 我想使用复选框来删除列表视图项。 有什么区别?您可以更改部分代码 【参考方案1】:重用 convertView 时必须重新加载检查状态。 setOnCheckedChangeListener(null) 很重要,因为在设置检查状态时会调用旧的监听器。
@Override
public View getView(final int position, View convertView, ViewGroup parent)
final int pos = position;
ViewHolder holder = null;
if (convertView == null)
holder = new ViewHolder();
LayoutInflater inflater = getLayoutInflater();
convertView = inflater.inflate(resource, null);
holder.chk = (CheckBox) convertView.findViewById(R.id.chk);
holder.txt = (TextView) convertView.findViewById(R.id.txt);
convertView.setTag(holder);
else
holder = (ViewHolder) convertView.getTag();
holder.txt.setText(items.get(position));
// you have to reload the check states
holder.chk.setOnCheckedChangeListener(null);
holder.chk.setChecked(ids.contains(position));
holder.chk
.setOnCheckedChangeListener(new OnCheckedChangeListener()
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked)
if (isChecked)
ids.add(position);
System.out.println("IDS A: " + ids.toString());
else
if (ids.contains(position))
int i = ids.indexOf(position);
ids.remove(i);
System.out.println("IDS R: "
+ ids.toString());
);
return convertView;
【讨论】:
当我使用 holder.chk.setOnCheckedChangeListener(null);然后从arraylist中删除多个项目,但不在正确位置删除项目。当我使用复选框从 listview 中选择 Chk 1 和 Chk 2 时,从 listview 2 中删除的 1 和 3 仍然存在。 还有其他方法可以实现吗?以上是关于使用android中的自定义arrayadapter在listview中使用复选框检查后如何删除多个项目?的主要内容,如果未能解决你的问题,请参考以下文章
Android:将数据添加到 ArrayAdapter 时可见的 ListView 图像闪烁
ListView 中自定义 ArrayAdapter 的自定义过滤