xiaoyan_12
xiaoyan_12
2012-11-09 09:15
采纳率: 84.9%
浏览 3.4k

在ListView中用复选框选择项目文件

程序的ListView视图中设置有复选框。我想从ListView中获取选中的项目,例如:MyFiles应用程序中用复选框选择多个文件,然后点击单个删除按钮就能删除所有的文件。有什么方法实现这样的功能呢?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • nana4ever
    nana4ever 2012-11-12 05:25
    已采纳
    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;
        }
    }
    

    然后检查:

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

    把selectedIds声明为全局变量。

    点赞 评论
  • csh_34
    csh_34 2012-11-12 02:41
    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 
       }
    
    点赞 评论
  • baimy1985
    balmy 2012-11-16 13:56

    关键是你要有一个集合来准确记录你选中的文件,删除时从集合里取出记录,调用你的删除方法就好了
    代码的写法就多了
    上面 nana4ever的回答是可以,应该就是那种思路,如果是4.0的系统也可以用下面这种,系统带的,也挺方便的,原理是一样的。

    ListView mList = getListView();
    ModeCallback multiListener = new ModeCallback();
    mList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
    mList.setMultiChoiceModeListener(multiListener);
    
    public class ModeCallback  implements ListView.MultiChoiceModeListene {}
    

    这个要实现必要的方法
    .

    点赞 评论

相关推荐