java - 多选 ListView 包含复选框

标签 java android

我有带有复选框的 ListView,应用程序删除选中的项目;;并且应该删除与选中的项目具有相同 id 的项目: 这是我的代码

private class CAdapter extends BaseAdapter {
        private LayoutInflater mInflater;
        private ArrayList<Entity> list;
        private Context context;
        String Status;
        CAdapter(Context context,
                ArrayList<Entity> getC) {
            this.context = context;
            this.list = getC;
             Status="";
            mInflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        class ViewHolder {
            TextView Name;
            TextView Desc;

            Button deleteBtn;

            CheckBox CBox;
        }

        public int getCount() {
            return list.size();
        }

        public Object getItem(int position) {
            return list.get(position);
        }

        public long getItemId(int position) {
            return position;
        }




        @SuppressLint("NewApi")
        public View getView(final int position, View convertView, ViewGroup parent) {

            final ViewHolder holder;
            final CEntity CObj = list.get(position);

            if (convertView == null) {
                convertView = mInflater.inflate(
                        R.layout.custom_list_view_confirmed, parent,
                        false);
                holder = new ViewHolder();


                holder.Name = (TextView) convertView
                        .findViewById(R.id.Name);



                holder.Desc = (TextView) convertView
                        .findViewById(R.id.activity1);





                holder.deleteBtn = (Button) convertView
                        .findViewById(R.id.deleteBtn);

                holder.CBox=(CheckBox) convertView.findViewById(R.id.isCheck);

                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }








            if (CObj.getMystatus().equals(
                    context.getResources().getString(R.string.course_status_delete))) {
                holder.status.setTextColor(Color.RED);
            } else if (attemptedCourseObj.getMystatus().equals(
                    context.getResources().getString(R.string.course_status_pending))) {
                holder.status.setTextColor(Color.GREEN);
            } else if (attemptedCourseObj.getMystatus().equals(
                    context.getResources().getString(R.string.course_status_update))) {
                holder.status.setTextColor(Color.BLUE);
            }

            holder.Name.setText(attemptedCourseObj.getCourseName());







            holder.CBox.setOnClickListener(new OnClickListener(){

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    if(holder.CBox.isChecked()){
                         if(list.contains(getItem(position))){
                             list.remove(getItem(position));
                             }
                    }
                }

            });
//          

            return convertView;
        }
    }

问题是当删除选中的项目时,不会删除具有相同 id 的项目。

int pos=Data.CList.size();
            SparseBooleanArray checked=CListView.getCheckedItemPositions();
             for (int n = pos; n > 0; n--){
                 if (checked.get(n)){
                 code=Data.inList.get(n).getCCode();
                 Data.inList.remove(n);

                 }else if(CList.get(n).equal(code){
Data.inList.remove(n);
}

最佳答案

尝试使用notifydatasetchanged刷新列表,并确保从列表中删除整个对象

关于java - 多选 ListView 包含复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22148242/

相关文章:

android - 滑动以打开选项 ListView

java - 音频样本混合或改变音量会导致饱和度和白噪声

java - 在 Java 中使用 Double.POSITIVE_INFINITY 寻找最小值

java - 需要澄清 - 设计模式

java - 如何跟踪 Java 异常

android - 在同一 View 上添加多个处理程序?

java - 从 MainActivity 调用具有参数上下文和属性集的方法

java - Groovy Array.addAll 方法从原始数组中删除元素

java - 下划线会改变整数的行为吗?

android - Android 中的外部 SD 卡(外部存储器)?