android]ListView+SimpleCursorAdapter+checkbox实现批量删除

[android]ListView+SimpleCursorAdapter+checkbox实现批量删除
2013-11-19 10:41:03       我来说两句         作者:RowandJJ
收藏    我要投稿
最近项目有个需求。
实现对笔记列表进行批量删除,功能本身实现比较容易。
网上也有很多demo参考。但是这个项目不太一样,因为使用的是SimpleCursorAdapter绑定ListView,网上大多数都是ArrayAdapter、SimpleAdapter、BaseAdapter的例子。故而这里有必要记录一下解决的办法。
 
 
先来看下别人怎么实现的:
实现批批量删除网上大致有以下几种方式:
1.ArrayAdapter+ListView+CheckBox:http://theopentutorials.com/tutorials/android/listview/android-multiple-selection-listview/
2.BaseAdapter+ListView+CheckBox:http://schimpf.es/listview-with-checkboxes-inside/
3.使用ViewBinder添加CheckBox:http://hi.baidu.com/xghhy/item/391caf9db4c36cdc1e427179
 
---------------------------------------------------------------------------
解决方法:
1.继承SimpleCursorAdapter,重写getView方法
2.在自定义的适配器中添加一个集合,保存被选中的listView条目的id
3.对外提供一个获取被选中条目id集合的方法
 
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
public class CheckBoxAdapter4TextNote extends SimpleCursorAdapter 
   
        private ArrayList<Integer> selection = new ArrayList<Integer>(); //记录被选中条目id 
        private int mCheckBoxId = 0 ; //listView条目的样式对应的xml资源文件名(必须包含checkbox) 
        private String mIdColumn; //数据库表的id名称 
        public CheckBoxAdapter4TextNote(Context context, int layout, Cursor c, 
                String[] from, int [] to, int checkBoxId, String idColumn, 
                int flags) 
       
            super (context, layout, c, from, to, flags); 
            mCheckBoxId = checkBoxId; 
            mIdColumn = idColumn; 
       
        @Override 
        public int getCount() 
       
            return super .getCount(); 
       
        @Override 
        public Object getItem( int position) 
       
            return super .getItem(position); 
       
        @Override 
        public long getItemId( int position) 
       
            return super .getItemId(position); 
       
        @Override 
        public View getView( final int position, View convertView, 
                ViewGroup parent) 
       
            View view = super .getView(position, convertView, parent); 
            final CheckBox checkbox = (CheckBox) view.findViewById(mCheckBoxId); 
            checkbox.setOnClickListener( new OnClickListener() 
           
                @Override 
                public void onClick(View v) 
               
                    Cursor cursor = getCursor(); 
                    cursor.moveToPosition(position); 
                      
                    checkbox.setChecked(checkbox.isChecked()); 
                    if (checkbox.isChecked()) //如果被选中则将id保存到集合中 
                   
                        selection.add(cursor.getInt(cursor.getColumnIndex(mIdColumn))); 
                   
                    else //否则移除 
                   
                        selection.remove( new Integer(cursor.getInt(cursor.getColumnIndex(mIdColumn)))); 
                        Toast.makeText(context, "has removed " + cursor.getInt(cursor.getColumnIndex(mIdColumn)), 0 ).show(); 
                   
               
            }); 
              
            return view; 
       
/返回集合 
        public ArrayList<Integer> getSelectedItems() 
       
            return selection; 
       
   

 

 
调用:
 
?
1
2
3
4
5
List<Integer> bn = XXX.getSelectedItems(); 
  for ( int id : bn) 
     //TODO 执行删除操作 
  }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值