views:

65

answers:

1

Hi all,

I'm trying to get multiple pieces of data from an SQLite database into a ListView but it doesn't seem to be working.

I'm using the code from developer.android.com's Notepad example and it works fine for 1 piece of data but not 2.

I'm trying to get the title and body of each database entry from the database, through a cursor and into a view, I think my problem is with the xml:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"&gt;
    <TextView android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="28dip" />
    <TextView android:id="@+id/text2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="28dip" />
</LinearLayout>

Here is my code for trying to bind the values:

mNotesCursor = mDbHelper.fetchAllNotes();
        startManagingCursor(mNotesCursor);
String[] from = new String[]{NotesDbAdapter.KEY_TITLE, NotesDbAdapter.KEY_BODY};
int[] to = new int[]{R.id.text1, R.id.text2};

SimpleCursorAdapter notes = 
        new SimpleCursorAdapter(this, R.layout.notes_row, mNotesCursor, from, to);
    setListAdapter(notes);

If I am only getting KEY_TITLE into an .xml with only one TextView (like in the Notepad tutorial) then it is fine, but if I try and run it with the xml defined above, it force closes.

Any ideas why?

Thanks for your time,

InfinitiFizz

A: 

It would be better if you create your custom adapter instead of using the "SimpleCursorAdapter".

basically, what you need to do is to create a new class that derives from "CursorAdapter" and then implement the "newView" and "bindView" methods. in both of these methods you get the cursor pointed to the correct position so you can access the contents directly and map between your view and the data.

yosig81