views:

83

answers:

2

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);

...

+2  A: 

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

Josiah
I don't see a CursorAdapter, I see a SimpleCursorAdapter, which I suspect is similar, but it is not the same. If you look at my example code, I'm using the CursorAdapter. I'm trying to display an image based upon the data in the database. In my case it's simply a different graphic if the value stored is positive or negative. If a SimpleCursorAdapter would work for this, then let me know. Otherwise, I'm assuming it won't do that.
Mitch
The documentation of the SimpleCursorAdapter says that it can map to ImageViews. http://developer.android.com/reference/android/widget/SimpleCursorAdapter.html
Josiah
I'm not using an ImageView, I'm using a ImageButton. Furthermore, I'm not sure how they would know how to map my values. How would it know if a positive or negative value would map to a different image on my ImageButton. Hence the reason I thought I would need to use a CursorAdapter and write the mapping myself. True?
Mitch
As far as I can see, yes. Sorry to not have been of much help :/
Josiah
A: 

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: http://stackoverflow.com/questions/2612581/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.

QRohlf