【问题标题】:ListView with SimpleCursorAdapter带有 SimpleCursorAdapter 的 ListView
【发布时间】:2011-09-06 06:06:06
【问题描述】:

在我的应用程序中,我使用列表视图来显示带有菜肴名称的菜肴菜单

这是我使用适配器将查询中的数据绑定到列表视图的代码:

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

我的查询代码如下所示:

public Cursor getAllDishes()
{
    return db.query(DATABASE_TABLE, new String[] {DishName},null,null,null,null,null);
}

非常感谢任何帮助/cmets,因为我已经坚持了将近一周。

【问题讨论】:

  • 我没有阅读代码,因为您可能需要改进您的问题。有什么问题,是什么导致你卡住了?
  • 通过 Listview 查看本教程:vogella.de/articles/AndroidListView/article.html
  • 对不起,我忘了添加,应用程序似乎无法使用第 1 篇文章中的代码运行,我做了一个 logcat 来检查查询是否有效并且它返回 true,这意味着值是空的吧?

标签: android listview simplecursoradapter


【解决方案1】:

你能清楚地解释你的问题是什么吗?

我的建议改变

    public Cursor getAllDishes()
{
    return db.query(DATABASE_TABLE, new String[] {DishName},null,null,null,null,null);
}

    public Cursor getAllDishes()
    {
    Cursor dishes = db.query(DATABASE_TABLE, new String[] {DishName},null,null,null,null,null);
if(dishes != null)
dishes.moveToFirst();
        return dishes;
    }

然后

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

【讨论】:

    猜你喜欢
    • 2012-08-03
    • 1970-01-01
    • 2011-08-20
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-29
    • 1970-01-01
    相关资源
    最近更新 更多