Reputation: 45
Can anyone suggest the best way to populate a list from an sqlite database. The database has multiple columns A, B, C, D, E, F, and multiple rows 1, 2, 3, 4, 5, 6, 7, 8,
I am trying to populate the list with the values from columns A, B, D, F, and row 1 or 2 or 3 or 4 etc... depending what rowid has previously been passed.
Upvotes: 0
Views: 1406
Reputation: 26535
Not sure I got the question right, Nevertheless
Cursor mCursor = DatabaseHelper.fetchItems();
startManagingCursor(mCursor);
String[] from = new String[]{"column 1", "column 2", "column 3"};
int[] to = new int[]{R.id.Row_TextView01, R.id.Row_TextView02, R.id.Row_TextView03};
SimpleCursorAdapter lists = new SimpleCursorAdapter(this, R.layout.row, mCursor, from, to);
setListAdapter(lists);
And the row.xml file is like this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/Row_TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/Row_TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/Row_TextView03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Hope it helps.
Upvotes: 1
Reputation: 10039
You could use SimpleCursorAdapter - http://developer.android.com/reference/android/widget/SimpleCursorAdapter.html
If you want you could refer to this tutorial for beginners - http://coenraets.org/blog/android-samples/androidtutorial/
Upvotes: 0