Android: Using SimpleCursorAdapter to get Data from Database to ListView Android: Using SimpleCursorAdapter to get Data from Database to ListView android android

Android: Using SimpleCursorAdapter to get Data from Database to ListView


Using the database format in the tutorial that you linked to, every row has an _id, isbn, title, and publisher. Now let's assume that you want to display every title in a ListView:

db = new DBAdapter(this);SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,         android.R.layout.simple_list_item_1,         db.getAllTitles(),         new String[] { "title" },         new int[] { android.R.id.text1 });ListView listView = (ListView) findViewById(R.id.list);listView.setAdapter(adapter);

(You don't need to loop through the Cursor yourself, an adapter does this work for you!)

The last two parameters in your SimpleCursorAdapter constructor are what you are missing. They are the "from" and "to" parameters:

  • We want to get the name of each book which is stored in the column name title, this is where we get the information "from".
  • Next we need to tell it where "to" go: android.R.id.text1 is a TextView in the android.R.layout.simple_list_item_1 layout. (You can dig through your SDK and see the simple_list_item_1.xml file yourself or just trust me for the moment.)

Now both the "from" and "to" parameters are arrays because we can pass more than one column of information, try this adapter as well:

SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,         android.R.layout.simple_list_item_2,         db.getAllTitles(),         new String[] { "title", "publisher" },         new int[] { android.R.id.text1, android.R.id.text2 });

With this adapter the books in their database will displayed by title, then publisher. All we had to do is use a layout android.R.layout.simple_list_item_2 that takes two fields and define which columns go to which TextViews.

I hope that helps a little. There's plenty more to learn but maybe this will give you some basics.


Last Comment

Off the top of my head, to refresh the ListView after adding new data try this:

public void onClick(DialogInterface dialog, int which) {    String selection = getResources().getStringArray(R.array.markslist)[which];    db.insertMark(date, "Default", selection);    cursor.requery();    adapter.notifyDataSetChanged();}

You'll have to define adapter and create a variable for cursor but that's simple:

public class Marks extends ListActivity {    SimpleCursorAdapter adapter;    Cursor cursor;    DBAdapter db = new DBAdapter(this);    ...

And change getData() accordingly:

private void getData() {    cursor = db.getAllMarks();    adapter = new SimpleCursorAdapter(this,             android.R.layout.simple_list_item_1,             cursor,             new String[] { "value" },             new int[] { android.R.id.text1 });    ...}

Good luck!


An adapter isn't used on each item in the cursor, one adapter is created for the entire cursor.You can set this listview to use that cursor. Try some SimpleCursorAdapter tutorials like this