Domanda

I have a ListView with android:choiceMode="multipleChoice". I fill this ListView from a Cursor through a SimpleCursorAdapter. Is there really no way to directly bind the "CheckBox" of the ListView's CheckedTextView layout to a boolean value from the cursor?

Currently I loop through the cursor calling ListView.setItemChecked() if the value is true:

private void showMyData(long myId) {
    // fill the list
    String[] fromColumns = { "myTextColumn" };
    int[] toViews = { android.R.id.text1 };
    Cursor myCursor = _myData.readData(myId);
    CursorAdapter myAdapter = new SimpleCursorAdapter(this,
        android.R.layout.simple_list_item_multiple_choice,
        myCursor, fromColumns, toViews);
    ListView myListView = (ListView) findViewById(R.id.myListView);
    myListView.setAdapter(myAdapter);
    // mark items that include the object specified by myId
    int myBooleanColumnPosition = myCursor
        .getColumnIndex("myBooleanColumn");
    for (int i = 0; i < myCursor.getCount(); i++) {
        myCursor.moveToPosition(i);
        if (myCursor.getInt(myBooleanColumnPosition ) == 1) {
            myListView.setItemChecked(i, true);
        }
    }
}

That does the job. But I would like to have code like this:

String[] fromColumns = { "myTextColumn", "myBooleanColumn" };
int[] toViews = { android.R.id.text1, android.R.id.Xyz };

and have no loop. Am I missing something here or is it Android?

EDIT: I tried this as suggested by Luksprog:

public boolean setViewValue(View view, Cursor cursor,
        int columnIndex) {
    CheckedTextView ctv = (CheckedTextView) view;
    ctv.setText(cursor.getString(cursor
            .getColumnIndex("myTextColumn")));
    if (cursor.getInt(cursor.getColumnIndex("myBooleanColumn")) == 1) {
        ctv.setChecked(true);
        Log.d("MY_TAG", "CheckBox checked");
    }
    return true;
}

That logged checking the CheckBox but didn't actually do it. Maybe that's a bug on my side. And while it's even more complicated than the initial loop at least it feels like one is using the framework, not working against it. So thank you Luksprog for the answer.

But to sum it up: Android is actually missing the straight forward approach.

È stato utile?

Soluzione

Use a SimpleCursorAdapter.ViewBinder on your adapter. Make sure your Cursor has the boolean values column in it and then:

String[] fromColumns = { "myTextColumn" };
int[] toViews = { android.R.id.text1 };
Cursor myCursor = _myData.readData(myId);
CursorAdapter myAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_multiple_choice, myCursor, fromColumns, toViews);
myAdapter.setViewBinder(new ViewBinder() {
       public boolean setViewValue (View view, Cursor cursor, int columnIndex) {
          // set the text of the list row, the view parameter (simple use cursor.getString(columnIndex))
          // set the CheckBox status(the layout used in the adapter is a CheckedTextView)
          return true;
       }
});
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top