我的自定义适配器活动:
复制代码
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
61private static HashMap<Integer, Boolean> checkedMap; private class MyMediaCursorAdapter extends ResourceCursorAdapter { public MyMediaCursorAdapter(Context context, int layout, Cursor c) { super(context, layout, c); } @Override public View newView(Context context, Cursor cursor, ViewGroup parent) { View view = super.newView(context, cursor, parent); final RecordListItemCache cache = new RecordListItemCache(); cache.date = (TextView)view.findViewById(R.id.dateRecorded); cache.checkBox = (CheckBox)view.findViewById(R.id.checkBoxView); view.setTag(cache); return view; } @Override public void bindView(View view, Context context, Cursor cursor) { final RecordListItemCache cache = (RecordListItemCache)view.getTag(); final SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss"); final String dateString = sdf.format(cursor.getLong(cursor .getColumnIndex(MediaStore.Audio.Media.DATE_ADDED))*1000); final String displayName = cursor.getString(cursor .getColumnIndex(MediaStore.Audio.Media.TITLE)); final int id = cursor.getInt(cursor .getColumnIndex(MediaStore.Audio.Media._ID)); cache.checkBox.setOnCheckedChangeListener(null); Boolean chk = checkedMap.get(id); if (chk==null) { cache.checkBox.setChecked(false); } else { cache.checkBox.setChecked(chk); } cache.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { checkedMap.put(id, isChecked); } }); cache.checkBox.setText(displayName); cache.date.setText(dateString); } } final static class RecordListItemCache { public CheckBox checkBox; public TextView date; public boolean checked; }
In onCreate in the same activity:
在onCreate中进行相同的活动:
复制代码
1
2
3
4myMediaCursorAdapter = new MyMediaCursorAdapter(getApplicationContext(), R.layout.multipledeleteview, audioCurosr); setListAdapter(myMediaCursorAdapter);
And onClick:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22public void onClick(View button) { switch (button.getId()) { case R.id.buttonDeleteMultiple: { Collection<Integer> IDs = checkedMap.keySet(); for (Integer id : IDs) { if (checkedMap.get(id)==true) { RecorderUtils.delete(getApplicationContext(), id, false); } } checkedMap = new HashMap<Integer, Boolean>(); } case R.id.buttonDeleteMultipleChooseAll: { int size = myMediaCursorAdapter.getCount(); ListView lv = getListView(); for(int i = 0; i <= size; i++) { lv.setItemChecked(i, true); } myMediaCursorAdapter.notifyDataSetChanged(); } } }
Delete is working fine (it deletes checked-ones). But button for choosing all doesn't change anything.
删除工作正常(删除已检查的)。但是选择所有的按钮不会改变任何东西。
最后
以上就是爱笑蚂蚁最近收集整理的关于Android ListView选中所有复选框(自定义ResourceCursorAdapter)的全部内容,更多相关Android内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复