Android SimpleCursorAdapter问题

发布于 2024-11-18 23:28:47 字数 1106 浏览 3 评论 0原文

我在 My DataBaseHelper 类中编写了此方法:

public Cursor fetchData(String tableName) {
    return myDataBase.rawQuery("SELECT rowid as _id, title FROM "+tableName, null);
}

并在我的活动之一中编写了此代码:

    try {
        Cursor cursor = myDbHelper.fetchData("tableName");
        String[] columns = {cursor.getColumnName(0), cursor.getColumnName(1)}; 
        int[] columnsLayouts = {R.id.layout1, R.id.layout2}; 
        SimpleCursorAdapter ca = new SimpleCursorAdapter
                            (this.getBaseContext(),
                            android.R.id.list, cursor,columns , columnsLayouts);
        lv.setAdapter(ca); //lv is my ListView with id="@android:id/list"
        txt.setText("Done ! : "); //Process is Ok

    } catch (Exception e){
        txt.setText("Error"); //Error happens
    }

代码编译正常。但是,当到达此行 lv.setAdapter(ca); 时,它会强制关闭。

有问题吗? 我做错了吗?

-

更新: 这是 logCat 错误(在我通过以下方式过滤它们之后: android.view

LogCat screen

I wrote this method in My DataBaseHelper class:

public Cursor fetchData(String tableName) {
    return myDataBase.rawQuery("SELECT rowid as _id, title FROM "+tableName, null);
}

And wrote this code inside one of my activities:

    try {
        Cursor cursor = myDbHelper.fetchData("tableName");
        String[] columns = {cursor.getColumnName(0), cursor.getColumnName(1)}; 
        int[] columnsLayouts = {R.id.layout1, R.id.layout2}; 
        SimpleCursorAdapter ca = new SimpleCursorAdapter
                            (this.getBaseContext(),
                            android.R.id.list, cursor,columns , columnsLayouts);
        lv.setAdapter(ca); //lv is my ListView with id="@android:id/list"
        txt.setText("Done ! : "); //Process is Ok

    } catch (Exception e){
        txt.setText("Error"); //Error happens
    }

The code is compiling ok. However, it closed forcefully when reaching this line lv.setAdapter(ca);.

Is there a problem?
did I do it the wrong way?

-

UPDATE:
this is the logCat errors (After I filter them by: android.view)

LogCat screen

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

余生共白头 2024-11-25 23:28:47

当尝试扩充列表视图中的项目时,Android 会崩溃。 构造函数的第二个参数是布局 -

layout - 定义此列表项的视图的布局文件的资源标识符。布局文件应至少包含“to”中定义的命名视图

您正在传递 android.R.id.list,这对我来说听起来像是您正在传递列表 id 而不是项目的 id。

Android crashes when trying to inflate the items in your list view. The second parameter of the constructor is layout -

layout - resource identifier of a layout file that defines the views for this list item. The layout file should include at least those named views defined in "to"

You are passing android.R.id.list which sounds to me like you are passing the list id instead of item's id.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文