我是靠谱客的博主 爱笑蚂蚁,最近开发中收集的这篇文章主要介绍Android ListView选中所有复选框(自定义ResourceCursorAdapter),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

我的自定义适配器活动:

private 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中进行相同的活动:

myMediaCursorAdapter = new MyMediaCursorAdapter(getApplicationContext(),
            R.layout.multipledeleteview, audioCurosr);
setListAdapter(myMediaCursorAdapter);

And onClick:

public 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 ListView选中所有复选框(自定义ResourceCursorAdapter)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(36)

评论列表共有 0 条评论

立即
投稿
返回
顶部