Reputation: 1052
I want to retrieve data from DB. And in my In my DBAdapter, I use fetchAllBooks
method to get a cursor:
public Cursor fetchAllBooks()
{
String selectQuery = "SELECT " + TABLE_TITLE + "." + KEY_TITLE_ID + " As _id, "
+ KEY_TITLE + ", " + KEY_PRICE + ", " + KEY_ISBN
+", GROUP_CONCAT(" + KEY_AUTHOR + ", ', ') AS authors"
+ " FROM " + TABLE_TITLE + " JOIN " + TABLE_AUTHOR
+ " USING (" + KEY_TITLE_ID + ")"
+ " GROUP BY " + KEY_TITLE_ID;
Cursor c = db.rawQuery(selectQuery, null);
return c;
}
In the main activity , I use fillData
method to fill the ListView:
private void fillData() {
Cursor c = db.fetchAllBooks();
startManagingCursor(c);
Log.d("count", c.getCount() + "");// could display the number of book. such as: count: 2; --- means cursor have 2 row(books)
if(c.moveToFirst()) {
do {
Log.d("title", BookContract.getTitle(c)); // could display the title of each book;
}while(c.moveToNext());
}
String[] from = new String[] {BookContract.KEY_TITLE, BookContract.KEY_AUTHORS};
int[] to = new int[] { R.id.cart_row_title, R.id.cart_row_author};
adapter = new SimpleCursorAdapter(this, R.layout.cart, c, from, to, 0);
setListAdapter(adapter);
}
As you see, the LogCat could display the content of cursor. it means that I retrieve the cursor successfully. However, the listView cannot display the book title and the authors. I do not use any db.close()
or cursor.close()
in my code.
the cart_row.xml
which represent each row item in the list view.And the cart.xml
which contain a list view:
Thanks in advance.
Upvotes: 0
Views: 46
Reputation: 1052
I manage to answer my own question. A little funny...
please pay more attention to this sentence:
the
cart_row.xml
which represent each row item in the list view. And thecart.xml
which contain a list view:
The simpleCursorAdapter need cart_row.xml
instead of cart.xml
. So change simpleCursorAdapter to:
adapter = new SimpleCursorAdapter(this, R.layout.cart_row, c, from, to, 0);
Upvotes: 1