1

I have ListView with check boxes the app delete the checked items ;; and should delet teh items that have the same id with the checked items : here's my code

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;
        }
    }

the problem is when delete the checked it dosent delete the item that have the same 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);
}
Henk Langeveld
  • 8,088
  • 1
  • 43
  • 57
kura
  • 45
  • 1
  • 6

2 Answers2

0

try to refresh the list with notifydatasetchanged and be sure that you delete the entire object from the list

Lucian Novac
  • 1,255
  • 12
  • 18
0

You need to inform your adapter with the new modifications by giving the adapter the new dataset after removing the deleted elements from it and notify with the changes. You can do this as the following: 1)Add method into your adapter to set the new data as following:

public void setNewData(ArrayList<Entity> newEntities){
    this.list = newEntities;
}

2)From the activity or the fragment call the previous method with the new data and call this line to notify the adapter with the changes

myAdapter.setNewData(myNewEntities);
myAdapter.notifyDataSetChanges();

Read this answer for more info about NotifyDataSetChanges() method

Community
  • 1
  • 1
Sami Eltamawy
  • 9,874
  • 8
  • 48
  • 66