multiselection listView包含checkBoxes

时间:2014-03-03 13:36:55

标签: 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);
}

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

您需要通过在删除已删除的元素后向adapter新数据集提供adapter来通知您的adapter,并通知更改。您可以执行以下操作: 1)将方法添加到public void setNewData(ArrayList<Entity> newEntities){ this.list = newEntities; } 以设置新数据如下:

activity

2)从fragmentadapter使用新数据调用上一个方法并调用此行以通知myAdapter.setNewData(myNewEntities); myAdapter.notifyDataSetChanges(); 更改

NotifyDataSetChanges()

有关{{1}}方法

的详情,请阅读此answer