I read a tutorial, and it uses SQLlite and "SimpleCursorAdapter" to fill the list with items. This is the code the tutorial taught me.
private void fillData() {
// Get all of the notes from the database and create the item list
Cursor c = mDbHelper.fetchAllNotes();
startManagingCursor(c);
String[] from = new String[] { NotesDbAdapter.KEY_TITLE };
int[] to = new int[] { R.id.text1 };
// Now create an array adapter and set it to display using our row
SimpleCursorAdapter notes =
new SimpleCursorAdapter(this, R.layout.notes_row, c, from, to);
setListAdapter(notes);
}
However...what if I want to fill it with XML data? Is it the same method? Can someone give me an example (in code)? thanks.
The example is using a
CursorAdapter
because aCursor
object is returned by theNotesDbAdapter
(if i remember correctly )fetchAllNotes
method. I don't know if there is a way to pass in raw XML to create a list but you can use name/value pairs in aHashMap
to create a list using the SimplelistAdapter.You can parse your xml and or json and build a hash table with it and use that to populate a list. The following example doesn't use xml, in fact it's not dynamic at all, but it does demonstrate how to assemble a list at runtime. It's taken from the
onCreate
method of an activity that extendsListActivity
. The all uppercase values are static constant strings defined at the top of the class, and are used as the keys.