I’m having some problems populating a ListView in Android with a rawQuery command, I’m hoping some new eyes on it helps out.
I’m using a method to copy my own database over from the assets folder, which I believe is working correctly. The database is being copied over correctly, as I’m watching this happen in DDMS. I’m also getting log.i entries that are telling me the database is being opened properly as well. I’m not getting any errors when I Logcat, but nothing is populating my ListView.
Is there something wrong with my command below to query the DB and report back?
Any help would be much appreciated.
private void fillData() {
Cursor c = myDbHelper.rawQuery("SELECT name FROM sqlite_master", null);
startManagingCursor(c);
// Create an array to specify the fields we want to display in the list.
String[] from = new String[]{"name"};
// an array of the views that we want to bind those fields too.
int[] to = new int[]{R.id.listview};
// Now create a simple cursor adapter and set it to display
SimpleCursorAdapter notes =
new SimpleCursorAdapter(this, R.layout.list_view, c, from, to);
setListAdapter(notes);
}
Here is my lists.xml file where I want the results of the ListView to populate:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:text = "Lists"
style="@style/HeaderText"
android:textSize="15dp"
android:gravity="center" />
<View
android:layout_width="wrap_content"
android:background="@drawable/gradient"
android:layout_height="1dp" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText
android:id="@+id/searchText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<Button
android:id="@+id/searchButton"
android:text="Search"
android:background = "@drawable/main_buttons"
style="@style/ButtonText"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />"
</LinearLayout>
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<TextView android:id="@android:id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:text="No data"
style="@style/ButtonText"/>
</LinearLayout>
</LinearLayout>
And finally, the list_view layout is just a TextView to populate the correct list from the rawQuery command.
If you want to display the tables names in your list you could try this
fillData()method:ListViewrequires that the cursor on which is based to have an_idcolumn. But in that table you don’t have an_idto add to the query so you could try to parse the cursor from the raw query into anArrayListand then bind it to a simpleArrayAdapter.