ListView with SimpleCursorAdapter
in my app i am using a listview to display a menu of dishes with their Dish Names
This is the code where i bind the data from the query to the listview using the adapter:
private void getDishes() {
DBAdapter db = new DBAdapter(this);
db.open();
Cursor cursor = db.getAllDishes();
cursor.moveToFirst();
Log.w("ppp","kk - " + cursor.moveToFirst());
SimpleCursorAdapter adapter = new SimpleCu开发者_StackOverflow中文版rsorAdapter(this,
R.layout.list_item, cursor, new String[] { DBAdapter.DishName },
new int[] {R.id.dishName });
setListAdapter(adapter);
db.close();
}
My Query code looks like this:
public Cursor getAllDishes()
{
return db.query(DATABASE_TABLE, new String[] {DishName},null,null,null,null,null);
}
Really appreciate any help/comments as i have been stuck on this for almost a week now.
Can you explain explicitely what's your problem?
My suggest change
public Cursor getAllDishes()
{
return db.query(DATABASE_TABLE, new String[] {DishName},null,null,null,null,null);
}
to
public Cursor getAllDishes()
{
Cursor dishes = db.query(DATABASE_TABLE, new String[] {DishName},null,null,null,null,null);
if(dishes != null)
dishes.moveToFirst();
return dishes;
}
Then
in getDishes()
method write like this
private void getDishes() {
DBAdapter db = new DBAdapter(this);
db.open();
Cursor cursor = db.getAllDishes();
SimpleCursorAdapter adapter = new SimpleCursorAdapter(YourActivity.this,
R.layout.list_item, cursor, new String[] { DBAdapter.DishName },
new int[] {R.id.dishName });
setListAdapter(adapter);
db.close();
}
精彩评论