简体   繁体   English

具有SimpleCursorAdapter的ListView

[英]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 SimpleCursorAdapter(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 getDishes()方法中这样写

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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM