Skip to content Skip to sidebar Skip to footer

How To Delete Check Box Items From List

I have 50 list item in the list. Now i have checked 10 item, so how to this 10 checked item delete(remove) from the listview when I click delete button. Here is My Code Please see

Solution 1:

I performed the same task in one of my app.

What you should do is use an ArrayList having the same size as the list item and each item contains 0 by default . Now in your Efficient Adapter for each checkbox that is checked , assign 1 in that particular position in arrayList . Finally on the button click of delete , traverse through the ArrayList and delete all those item from list which contain 1 in that postion in that ArrayList and atLast call notifyDatasetChanged() method of list so that list get refreshed and shows the new list.

Here is some sample code to give you a better idea over this :

 ArrayList<Integer> checks=new ArrayList<Integer>();

Now in onCreate method

for(int b=0;b<tempTitle.length;b++){
    checks.add(b,0);  //assign 0 by default in each position of ArrayList
        } 

Now in your efficientAdapter's getView method

 public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.bookmarks_list_item,
                    null);
            holder = new ViewHolder();
            holder.text1 = (TextView) convertView
                    .findViewById(R.id.title);
            holder.text2 = (TextView) convertView
                    .findViewById(R.id.body);
             holder.checkBox = (CheckBox) convertView.findViewById(R.id.checkbox);

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

           holder.checkBox.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub

                if(((CheckBox)v).isChecked()){
                  checks.set(position, 1);
                }
                else{
                 checks.set(position, 0);
                }

            }
        });


        return convertView;
    }

Finally on delete Button click :

     delete.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
    // TODO Auto-generated method stub

          for(int i=0;i<checks.size();i++){

    if(checks.get(i)==1){
          //remove items from the list here for example from ArryList 
          checks.remove(i);
           //similarly remove other items from the list from that particular postion
           i--;
        }
     }
    ((EfficientAdapter)lv.getAdapter()).notifyDataSetChanged();
  }
}

Hope this will solve your issue.


Solution 2:

I am assuming i got ids which are selected. now simply arrayList.remove(position); and call notifyDataSetChanged();


Solution 3:

man, your code lacks the relation between the check box and the object and also you didn't implement onCheckedchangeListener inside your adapter .... here is what you need http://www.vogella.de/articles/AndroidListView/article.html#listadvanced_interactive Or I'll wrap it for you:

  1. you'll need to create a class model.java which represents the cell of the list as follows:

    public class Model {
         private String title;
         private String body;
         private boolean isSelected;
    
         public Model(String title, String body) {
              this.title = title;
              this.body = body;
              this.isSelected = false;
         }
    
         // here you MUST create your set of setters and getters.
    }
    
  2. modify your adapter to extend ArrayAdapter<Model>

  3. modify the constructor of the adapter to be

    private Model[] model;
    public EfficientAdapter(Context context, Model model) {
    
         mInflater = LayoutInflater.from(context);
         this.model = model;
    
    }
    
  4. then you'll need to add the onCheckedChangeListener inside your adapter inside the getView method , so your getView method will look like this:

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = null;
        if (convertView == null) {
            LayoutInflater inflator = context.getLayoutInflater();
    
            final ViewHolder viewHolder = new ViewHolder();
            viewHolder.text1 = (TextView) convertView.findViewById(R.id.title);
            viewHolder.text2 = (TextView) convertView.findViewById(R.id.body);
            viewHolder.checkBox = (CheckBox) convertView.findViewById(R.id.checkbox);
            viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    
                @Override
                public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
    
                    Model element = (Model) viewHolder.checkbox.getTag();
    
                    element.setSelected(buttonView.isChecked());
                }
            });
            view.setTag(viewHolder);
            viewHolder.checkbox.setTag(list[position]);
        } else {
            view = convertView;
            ((ViewHolder) view.getTag()).checkbox.setTag(list[position]);
        }
    
        ViewHolder holder = (ViewHolder) view.getTag();
        holder.text1.setText(tempTitle[position]);
        holder.text2.setText(tempBody[position]);
        return view;
    }
    
  5. then create the model array inside your activity and pass it to the adapter.

  6. the last thing to be done is to delete the selected items from the list:

    final ArrayList<Model> newModel = new ArrayList<Model>();
    
    for (int i = 0; i < model.length/*this is the original model*/; i++) {
    
        if(model[i].isChecked()){
    
            // fill the array list ...
            newModel.add(model[i]);
        }
    }
    
  7. that's it, pass the newModel to the adapter and rest the adapter to the list.

step number 7 can be performed also by filling the model(the original one which is passed originally to the array) and then call notifyDataSetChanged() using the adapter.

that's all and that's what always worked for me... hope this helps you too.


Solution 4:

If you use a listview with mode CHOICE_MODE_MULTIPLE, you should be able to use something like: listView.getCheckedItemPositions();

then if you have the list items stored in some kind of list or vector, you should be able to easily remove items with the same positions, and the updating the listview.

Altough all my checkbox + list knowledge is based on this: Android list and checkbox


Solution 5:

I think you problem does not need any big amount of code.You need just a concept.Answer Given above is best way of doing that.

I am assuming i got ids which are selected. now simply arrayList.remove(position);
and call notifyDataSetChanged();

As List view sub child(CheckBox) can use setOnCheckChangesListener in base Adapter class directly.Now you just have to keep track record of check checkbox and their corresponding position in ArrayList.Now when you click on delete btn then remove element from arraylist and call notifyDataSetChanged()


Post a Comment for "How To Delete Check Box Items From List"