Mitch
Mitch

Reputation: 1734

ListActivity with Cursor

Does anyone know of a simple example that uses the CursorAdapter? Here's what I'm doing now and it's crashing with a RuntimeException. I'm sure it something simple I'm missing given that I'm a newbie and can't find any simple examples of a ListView that uses a Cursor.

Thanks,


...

public final class MyListActivity extends ListActivity { private class MyCursorAdapter extends CursorAdapter { public MyCursorAdapter(Context context, Cursor cursor) { super(context, cursor); // CRASH ...

protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    myDB_ = new MyDB(this);
    myDB_.open();

    Cursor cursor = myDB_.read();
    startManagingCursor(cursor);

    MyCursorAdapter adapter = new MyCursorAdapter(this, cursor);

...

Upvotes: 1

Views: 2425

Answers (2)

QRohlf
QRohlf

Reputation: 2853

You can use setViewBinder on a SimpleCursorAdapter to map values to views not supported by the SimpleCursorAdapter itself. You can see an example of using setViewBinder to bind data from the content provider to a CheckBox here: CheckBox checked state in a ListView

You could use setViewBinder to bind your images to your imageButtons. That way, you don't have to create your own ListAdapter.

Upvotes: 0

Josiah
Josiah

Reputation: 4854

The Notepad tutorial in the Android developer resources uses a CursorAdapter with ListView. You can find the relevant part of the tutorial here: http://developer.android.com/resources/tutorials/notepad/notepad-ex1.html

Upvotes: 3

Related Questions