使用复选框从ListView中获取选中的项目

时间:2012-08-13 08:58:08

标签: android android-widget android-listview android-checkbox

我有一个ListView CheckBox。我想从ListView示例MyFiles应用程序中获取所选项目,我们选择带有CheckBox的多个文件,然后单击单个删除按钮删除所有文件。

4 个答案:

答案 0 :(得分:3)

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

检查====&gt;

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

selectedIds声明为全局变量

答案 1 :(得分:2)

非常简单的解决方案对我有用:

像这样,您可以使用ArrayAdapter的现有ArrayList遍历ListView。

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

答案 2 :(得分:1)

您需要在复选框中添加OnClickListener(在适配器中)。当按下按钮时,此侦听器必须使用您要删除的文件保持最新的列表。

类似的东西:

已检查=&gt;添加到列表中 UnChecked =&gt;从列表中删除

答案 3 :(得分:1)

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 
   }