删除选中的ListView项目 [英] Deleting checked ListView items

查看:110
本文介绍了删除选中的ListView项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图删除检查的ListView 项目。它的工作原理,如果只有一个项目检查,但是,如果有两个或三个然后该应用力关闭。在错误日志这样说的:


  

E / AndroidRuntime(2173):java.lang.IndexOutOfBoundsException:无效的位置2,大小为2


具有列表上的三个项目如果和删除2.任何人都可以纠正这个错误帮助吗?这里是code:

 公共无效删除(){
    btnDelete =(按钮)findViewById(R.id.btnDelete);
    btnDelete.setOnClickListener(新OnClickListener(){        公共无效的onClick(视图v){
            的for(int i = 0; I< list.getChildCount();我++){
                查看查看= list.getChildAt(I)                 CheckedTextView CV =(CheckedTextView)view.findViewById(R.id.checkList);
                 如果(cv.isChecked()){
                    Log.i(删除,adapter.getItem(ⅰ)的ToString()++ cv.toString());                     adapter.remove(adapter.getItem(I));
                 }
                 adapter.notifyDataSetChanged();            }
           Toast.makeText(getApplicationContext(),所选项目已清除,Toast.LENGTH_SHORT).show();
       }
    });
   }


解决方案

问题是你的for循环。你被 list.getChildCount迭代的for循环列表视图()这是不正确读取正确的API文档,以 adapter.getCount更换(); 它会解决这个问题。

I'm trying to delete ListView items that are checked. It works if there is only one item checked but if there are two or three then the app force closes. On the error log it says:

E/AndroidRuntime(2173): java.lang.IndexOutOfBoundsException: Invalid location 2, size is 2

When having three items on the list and deleting 2. Can anyone help with correcting this error? Here is the code:

    public void delete() {
    btnDelete = (Button) findViewById(R.id.btnDelete);
    btnDelete.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            for (int i = 0; i < list.getChildCount(); i++) {
                View view = list.getChildAt(i);

                 CheckedTextView cv = (CheckedTextView) view.findViewById(R.id.checkList);
                 if(cv.isChecked()){
                    Log.i("DELETE", adapter.getItem(i).toString()+"   "+cv.toString());

                     adapter.remove(adapter.getItem(i));
                 }
                 adapter.notifyDataSetChanged();

            }
           Toast.makeText(getApplicationContext(), "Selected Items Cleared", Toast.LENGTH_SHORT).show();
       }
    });
   }

解决方案

The problem is with your for loop. you are iterating list view in the for loop by list.getChildCount() which is not correct read the Api doc correctly, replace it with adapter.getCount(); it will solve the problem.

这篇关于删除选中的ListView项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆