Android:CheckedTextView + SQLiteのListView、アダプター付きのチェック・行を表示しますか?

StackOverflow https://stackoverflow.com/questions/4974927

質問

私は非常に単純な「TODO」リストを実装しようとしています。 私はCheckedTextViewのListViewを使用することを選択しました(ID checked_textを使用)、ListViewはコースのcoict_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);
.

これはうまく機能している、それはDBのすべての項目とそれぞれの項目を取得します 項目それはテキストdbadapter.nameとしてcheckedTextViewを作成します。

しかし、私が本当に必要なのはテキストとチェックされた状態の両方を得ることです、 そしてそれに応じて行をチェックしてください。

DBから状態を(整数0/1として)状態を取得できるとしましょう。 dbadapter.Checked ..を取得するカーソルアダプタを実装する方法 仕事は?

私はどこでも検索しました(たとえば、この質問これは混乱しているがこの問題を解決するために何も見つかりませんでした。 wah。 最初はとても簡単に見えました。

ありがとう、
ステファノ


@bert 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エントリ(最初に選択された2番目)がLISTをロードしたときに:

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() method DbAdapter.NAME列に対して呼び出された場合、またはそれがR.id.checked_text型ビューをチェックしている場合はチェックします。

  3. ではなく、ではなく、ターゲット列/ビューには、falseが返されているため、アダプタが列をバインド/表示される方法を表示します。

  4. ターゲット列/ビューの場合は、(CheckedTextView) viewのテキストとチェックボックスを設定してください。アダプタがビュー自体をバインドしようとしないように、trueを返すようになります。カーソルはクエリデータにアクセスしてチェックボックスをチェックするかどうかを判断する(DbAdapter.CHECKED)。

  5. ViewBinderをvia setviewbinder()

    これは私のビューバインダーの実装の1つです。それはチェコボックスではなく、テキストビューのファンシーフォーマットを実行するためのものではなく、それはあなたにアプローチのためのいくつかの考えを与えるべきです:

    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;
            }
        };
    
    .

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top