Question

I have a ListView with the CheckBox. I want to get the selected items from the ListView like example MyFiles application we selecting the multiple files with CheckBox and clicking the single delete button to delete all the files.

Was it helpful?

Solution

  boolean bulkflag = false;
  ListView reportslistview = (ListView) findViewById(android.R.id.list);
  public class MyAdapter extends SimpleAdapter {
    //private List<Table> tables;
    SharedPreferences prefs;
    private Activity activity;
    String val = "";

    //@SuppressWarnings("unchecked")
    public MyAdapter(Activity context, List<? extends Map<String, String>> tables, int resource, String[] from,
            int[] to) {
        super(context, tables, resource, from, to);
        //this.tables = (List<Table>) tables;
        activity    =   context;
    }

    public View getView(final int position, final View convertView, ViewGroup parent) {
        View row = super.getView(position, convertView, parent);
        if (row == null) 
        {
            LayoutInflater inflater = (LayoutInflater) activity
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.reports_list, null);
        }       

        final CheckBox cBox=(CheckBox)row.findViewById(R.id.cb1);
        if(bulkflag)
        {
            cBox.setVisibility(View.VISIBLE);
        }
        else
        {
            cBox.setVisibility(View.GONE);
        }
        cBox.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

                if(cBox.isChecked())
                {
                    selectedIds.add(recIdArr.get(reportslistview.getPositionForView(cBox)));
                    //System.out.println("position "+reportslistview.getPositionForView(cBox));
                }
                else
                {
                    selectedIds.remove(recIdArr.get(reportslistview.getPositionForView(cBox)));
                }
            }
        });
        return row;
    }
}

Checking ====>

 for(int i=0;i<selectedIds.size();i++)
 {
    System.out.println("delete multiple"+selectedIds.size()+" "+Integer.parseInt(selectedIds.get(i)));
 }

Declare selectedIds as a global variable

OTHER TIPS

Pretty simple solution which works for me:

Like this you can iterate through the ListView by using the already extisting ArrayList for the ArrayAdapter.

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

if((CheckBox)listView.getChildAt(i).findViewById(R.id.checkBox) != null){

    CheckBox cBox=(CheckBox)listView.getChildAt(i).findViewById(R.id.checkBox);

    if(cBox.isChecked()){
        Log.e("CB", ""+i);
    }
 }
}  

You need to add a OnClickListener to your checkboxes ( in your adapter ). This listener will have to keep up to date a list with the file you want to delete when the button is pressed.

Something like :

Checked => Add to the List
UnChecked => Remove from List

Listview list;

ArrayAdapter<String> aa=new ArrayAdapter<String>this,android.R.layout.simple_list_item_multiple_choice,"Arraylist name");

list.setAdapter(aa);
list.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
     //code 
   }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top