Android Database Query Display
/This is part of the Android Quick Code Access series of posts. The code below displays the results of a Database Query. You'll need to run the Query code in a background thread in your production version.
// Database Display Activity. public class DatabaseDisplayActivity extends ListActivity { // Database display columns. private static final String[] PROJECTION = new String[] { COLUMN_NAME_1, // 0 Position ... } @Override // Activity onCreate method. protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Database Query. Cursor cursor = managedQuery( MyContent.CONTENT_URI, PROJECTION, null, null, MyContent.DEFAULT_SORT_ORDER ); // Cursor columns to display. String[] dataColumns = { COLUMN_NAME_1, ... } ; // View IDs for formatting. int[] viewIDs = { android.R.id.text1, ... }; // Adapter for the ListView. SimpleCursorAdapter adapter = new SimpleCursorAdapter( this, android.R.layout.simple_list_item_1, cursor, dataColumns, viewIDs ); // Binder to display row results. adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() { public boolean setViewValue( View view, Cursor cursor, int columnIndex) { // Get row of cursor. String displayText = DatabaseUtils.dumpCurrentRowToString(cursor); // Set text in display. ((TextView) view).setText(displayText); return true; } }); // List Adapter. setListAdapter(adapter); } }