So I have been trying to get a ListView to display data from a SQLite Database for a few days now, and I still can’t find a solution.
the ListView XML looks like:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/gotoEntry"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="New Entry" />
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@android:id/list" >
</ListView>
</LinearLayout>
with the row item looking like this:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="15dp"
android:padding="15dp"
android:id="@+id/thetext"
>
</TextView>
My cursoradapter looks like:
public class ItemAdapter extends CursorAdapter{
private Cursor iCursor;
private Context iContext;
private final LayoutInflater iInflater;
public ItemAdapter(Context context, Cursor c) {
super(context, c);
iInflater=LayoutInflater.from(context);
iContext=context;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView itemName = (TextView) view.findViewById(R.id.thetext);
itemName.setText(cursor.getString(cursor.getColumnIndex(db_Setup.KEY_NAME)));
}
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) throws InflateException {
final View view = iInflater.inflate(R.layout.single_item, parent, false);
return view;
}
}
The cursor is being made with this method:
public static Cursor getName() {
Cursor q = myDatabase.query(DATABASE_TABLE, new
String[] {db_Setup.KEY_NAME}, null, null, null, null, null);
q.moveToFirst();
return q;
}
and instantiated in the listactivity like so:
Cursor cursor = db_Setup.getName();
I am trying to do this in my ListActivity to bind the listview to the cursor:
ItemAdapter ia = new ItemAdapter(getBaseContext(), cursor);
ia.bindView(findViewById(R.id.thetext), getApplicationContext(), cursor);
There are no errors when I run the app… all there is a button from the XML at the top of this post, and blankness (no listView)…
I have entered some values in the SQLite Database already with something like 14 values or so. Why isn’t anything showing?
The last line is incorrect. After you create ItemAdapter, simply call ListActivity.setListAdapter(ia). See Displaying query results for more details.