Android: CheckedTextView + SQLite的ListView,用Adapter显示选中的行?

标签 android sqlite listview cursor checkedtextview

我正在尝试实现一个非常简单的“待办事项”列表。 我选择使用 CheckedTextView 的 ListView(id 为 checked_text),ListView 当然使用 CHOICE_MODE_MULTIPLE。

现在,我正在使用 SQLite 数据库来存储列表项。

要填写列表,我有类似的内容:

cursor = dbHelper.fetchAll();
startManagingCursor(cursor);

String[] from = new String[] {  DbAdapter.NAME };
int[] to = new int[] { R.id.checked_text };

SimpleCursorAdapter notes = new SimpleCursorAdapter(this,
R.layout.todo_multi_row, cursor, from, to);
listView.setAdapter(notes);

这工作正常,它获取数据库中的所有项目以及每个项目 item 它创建一个 CheckedTextView,文本为 DbAdapter.NAME。

但我真正需要的是获取文本和选中状态, 并相应地检查行。

假设我可以使用以下方法从数据库中获取状态(作为整数 0/1) DbAdapter.CHECKED.. 我怎样才能实现一个游标适配器来获取 工作完成了吗?

我到处搜索(例如 this question 是一团糟)但没有找到解决这个问题的方法。哇。 起初看起来很容易。

谢谢,
斯特凡诺


@伯特F:
谢谢,这是我用来填写列表的方法:

private void fillData() {
        Log.d("LIST","fill");
        cursor = dbHelper.fetchAllTodos();
        startManagingCursor(cursor);

        String[] from = new String[] { TodoDbAdapter.NAME };
        int[] to = new int[] { R.id.checked_text };

        SimpleCursorAdapter notes = new SimpleCursorAdapter(this,
                R.layout.todo_multi_row, cursor, from, to);

        final SimpleCursorAdapter.ViewBinder mViewBinder =
            new SimpleCursorAdapter.ViewBinder() {
                @Override
                public boolean setViewValue(
                        final View view,
                        final Cursor cursor,
                        final int columnIndex) {

                    Log.d("LIST",view +" "+cursor+" "+columnIndex);

                    CheckedTextView item = (CheckedTextView) view;
                    Log.d("LIST","NAME: "+item.getText()+" State: "+item.isChecked());

                    return false;
                }
            };

        notes.setViewBinder(mViewBinder);
        listView.setAdapter(notes);
    }

现在,作为测试,我只是打印出正在发生的事情。
如果我向列表中添加 1 个元素,我看到的是:

02-12 14:45:35.913: DEBUG/LIST(22621): fill
02-12 14:45:35.933: DEBUG/LIST(22621): android.widget.CheckedTextView@479da398 android.database.sqlite.SQLiteCursor@479d98f0 2
02-12 14:45:35.933: DEBUG/LIST(22621): NAME:  State: false
02-12 14:45:35.933: DEBUG/LIST(22621): android.widget.CheckedTextView@479da398 android.database.sqlite.SQLiteCursor@479d98f0 2
02-12 14:45:35.943: DEBUG/LIST(22621): NAME: first item State: false
02-12 14:45:36.013: DEBUG/LIST(22621): android.widget.CheckedTextView@479dc6c0 android.database.sqlite.SQLiteCursor@479d98f0 2
02-12 14:45:36.013: DEBUG/LIST(22621): NAME:  State: false
02-12 14:45:36.223: DEBUG/LIST(22621): android.widget.CheckedTextView@479dc6c0 android.database.sqlite.SQLiteCursor@479d98f0 2
02-12 14:45:36.223: DEBUG/LIST(22621): NAME: first item State: false

如果我检查它..

02-12 14:53:33.123: DEBUG/LIST(22621): android.widget.CheckedTextView@479dc6c0 android.database.sqlite.SQLiteCursor@479d98f0 2
02-12 14:53:33.123: DEBUG/LIST(22621): NAME: first item State: false
02-12 14:53:33.123: DEBUG/LIST(22621): android.widget.CheckedTextView@479da398 android.database.sqlite.SQLiteCursor@479d98f0 2
02-12 14:53:33.123: DEBUG/LIST(22621): NAME: first item State: false

撇开它说它没有被检查的事实不谈,为什么它会触发这么多次?我只有 1 项:我


编辑:丑陋的解决方案

private void fillData() {
        Log.d("LIST","fill");
        cursor = dbHelper.fetchAllTodos();
        startManagingCursor(cursor);

        String[] from = new String[] { TodoDbAdapter.NAME };
        int[] to = new int[] { R.id.checked_text };

        SimpleCursorAdapter notes = new SimpleCursorAdapter(this,
                R.layout.todo_multi_row, cursor, from, to);

        final SimpleCursorAdapter.ViewBinder mViewBinder =
            new SimpleCursorAdapter.ViewBinder() {
                @Override
                public boolean setViewValue(
                        final View view,
                        final Cursor cursor,
                        final int columnIndex) {

                     final int checkedIndex =
                         cursor.getColumnIndexOrThrow(
                                 TodoDbAdapter.CHECKED);

                     Log.d("LIST","VIEW: "+view+" NAME: "+cursor.getString(columnIndex)+" "+cursor.getInt(checkedIndex));

                    return false;
                }
            };

        notes.setViewBinder(mViewBinder);
        listView.setAdapter(notes);
    }

当我加载列表时有 2 个条目(首先选择第二个不是):

02-12 15:59:48.613: DEBUG/LIST(23533): fill
02-12 15:59:48.643: DEBUG/LIST(23533): VIEW: android.widget.CheckedTextView@47a30ec0 NAME: test 1
02-12 15:59:48.653: DEBUG/LIST(23533): VIEW: android.widget.CheckedTextView@47a30ec0 NAME: LOL 0
02-12 15:59:48.683: DEBUG/LIST(23533): VIEW: android.widget.CheckedTextView@47a30ec0 NAME: test 1
02-12 15:59:48.683: DEBUG/LIST(23533): VIEW: android.widget.CheckedTextView@47a331f8 NAME: LOL 0
02-12 15:59:48.713: DEBUG/LIST(23533): VIEW: android.widget.CheckedTextView@47a35770 NAME: test 1
02-12 15:59:48.713: DEBUG/LIST(23533): VIEW: android.widget.CheckedTextView@47a35770 NAME: LOL 0
02-12 15:59:48.783: DEBUG/LIST(23533): VIEW: android.widget.CheckedTextView@47a35770 NAME: test 1
02-12 15:59:48.783: DEBUG/LIST(23533): VIEW: android.widget.CheckedTextView@47a35770 NAME: LOL 0

干杯。

最佳答案

  1. 实现 SimpleCursorAdapter.ViewBinder .

  2. 拥有 ViewBinder.setViewValue()方法检查何时调用 DbAdapter.NAME 列,或者让它检查 R.id.checked_text 类型的 View 。

  3. 当它调用时,但不是针对不是目标列/ View ,让它返回false,这样适配器就会绑定(bind)列/ View 正常情况下的方式。

  4. 当它调用目标列/ View 时,让它为(CheckedTextView) View 设置文本和复选框。它最终应该返回 true,这样适配器就不会继续尝试绑定(bind) View 本身。请注意,游标可用于访问查询数据以确定是否选中复选框 (DbAdapter.CHECKED)。

  5. 通过setViewBinder() 在您的SimpleCursorAdapter 中设置您的ViewBinder

这是我的 ViewBinder 实现之一。它不是用于复选框,而是用于对 TextView 进行一些奇特的格式化,但它应该让您对这种方法有所了解:

private final SimpleCursorAdapter.ViewBinder mViewBinder =
    new SimpleCursorAdapter.ViewBinder() {
        @Override
        public boolean setViewValue(
                final View view,
                final Cursor cursor,
                final int columnIndex) {
            final int latitudeColumnIndex =
                cursor.getColumnIndexOrThrow(
                        LocationDbAdapter.KEY_LATITUDE);
            final int addressStreet1ColumnIndex =
                cursor.getColumnIndexOrThrow(
                        LocationDbAdapter.KEY_ADDRESS_STREET1);

            if (columnIndex == latitudeColumnIndex) {

                final String text = formatCoordinates(cursor);
                ((TextView) view).setText(text);
                return true;

            } else if (columnIndex == addressStreet1ColumnIndex) {

                final String text = formatAddress(cursor);
                ((TextView) view).setText(text);
                return true;

            }

            return false;
        }
    };

关于Android: CheckedTextView + SQLite的ListView,用Adapter显示选中的行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4974927/

相关文章:

android - Outlined TextInputLayout 渲染不正确

android - 如何在android中制作完整的圆形 ImageView

安卓 ListView 。对于宽列表,垂直滚动条并不总是可见

android - 以编程方式替换选择器图像

python - SQlite:保存/注册子查询以供重复使用

ios - Sqlite 文件位置核心数据

android - 如何在 ListView Adapter Android Studio 中设置自定义字体?

c# - sqlite数据库位置uwp

android - 从多选 ListView 返回值

android - 如何在项目单击事件的 ListView 附近显示弹出菜单?