创建 simplecursor 适配器时强制关闭

发布于 2024-11-04 10:39:36 字数 3621 浏览 5 评论 0原文

我有以下代码,应该在列表中显示数据库的内容(某个表和某个列),但我得到一个强制关闭对话框,我不知道为什么。

这是我的代码:

public class Server8 extends Activity {

DBAdapter db;

    @Override
    public void onCreate (Bundle savedInstanceState) {

       setContentView(R.layout.main);

       db=new DBAdapter(this);
       db.openDataBase();
       Cursor cursor=db.getAllData2();

       startManagingCursor(cursor);

       String[] from=new String[] {db.KEY_ROUTE};     

       int[] to = new int[] { R.id.name_entry};

       ContactListCursorAdapter items = new ContactListCursorAdapter(this, R.layout.list_example_entry, cursor, from, to);
    }

    public class ContactListCursorAdapter extends SimpleCursorAdapter{

        private Context context;

        private int layout;

        public ContactListCursorAdapter (Context context, int layout, Cursor c, String[] from, int[] to) {
            super(context, layout, c, from, to);
            this.context = context;
            this.layout = layout;
        }

        @Override
        public View newView(Context context, Cursor cursor, ViewGroup parent) {

            Cursor c = getCursor();

            final LayoutInflater inflater = LayoutInflater.from(context);

            View v = inflater.inflate(layout, parent, false);

            int nameCol = c.getColumnIndex(db.KEY_ROUTE);
            String name = c.getString(nameCol);

            TextView name_text = (TextView) v.findViewById(R.id.name_entry);
            if (name_text != null) {
                name_text.setText(name);
            }

            return v;
        }

        @Override
        public void bindView(View v, Context context, Cursor c) {

            int nameCol = c.getColumnIndex(db.KEY_ROUTE);
            String name = c.getString(nameCol);

            TextView name_text = (TextView) v.findViewById(R.id.name_entry);

            if (name_text != null) {
                name_text.setText(name);
            }
        }

在我的 DBAdapter 中,我有这个:

public static final String TABLE_2= "route";

public static final String KEY_ROWID_2="_id";

public static final String KEY_ROUTE= "route";

public static final String KEY_USER_ID= "user_id";

这是我查询光标的方式:

public Cursor getAllData2() 
{
    return db.query(TABLE_2, new String[] {KEY_ROUTE},null,null,null,null,null);
}

这就是我的 logcat 给我的:

04-30 09:26:24.323: ERROR/AndroidRuntime(2676): Caused by: java .lang.IllegalArgumentException:列“_id”不存在

04-30 09:26:24.323:错误/AndroidRuntime(2676):在android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:314)

04-30 09:26: 24.323:错误/AndroidRuntime(2676):在android.widget.CursorAdapter.init(CursorAdapter.java:111)

04-30 09:26:24.323:错误/AndroidRuntime(2676):在android.widget.CursorAdapter。(CursorAdapter。 java:90)

04-30 09:26:24.323: 错误/AndroidRuntime(2676): 在 android.widget.ResourceCursorAdapter.(ResourceCursorAdapter.java:47)

04-30 09:26:24.323: 错误/AndroidRuntime(2676):在 android.widget.SimpleCursorAdapter.(SimpleCursorAdapter.java:88)

04-30 09:26:24.323: 错误/AndroidRuntime(2676): 在 com.server.Server8$ContactListCursorAdapter.(Server8.java:103)

04-30 09 :26:24.323:错误/AndroidRuntime(2676):在com.server.Server8.onCreate(Server8.java:91)


引起:java.lang.IllegalArgumentException:列'_id'不存在

我什至没有要求像这样的东西!

这就是我的 TABLE_2 的样子:

_id         route                  user_id

1           Sebes-Alba               1             ....only one record

我做错了什么?谢谢

I have the following code which should display in a list the content from a database (a certain table and a certain column) but I get a force close dialog and I don't know why.

This is my code:

public class Server8 extends Activity {

DBAdapter db;

    @Override
    public void onCreate (Bundle savedInstanceState) {

       setContentView(R.layout.main);

       db=new DBAdapter(this);
       db.openDataBase();
       Cursor cursor=db.getAllData2();

       startManagingCursor(cursor);

       String[] from=new String[] {db.KEY_ROUTE};     

       int[] to = new int[] { R.id.name_entry};

       ContactListCursorAdapter items = new ContactListCursorAdapter(this, R.layout.list_example_entry, cursor, from, to);
    }

    public class ContactListCursorAdapter extends SimpleCursorAdapter{

        private Context context;

        private int layout;

        public ContactListCursorAdapter (Context context, int layout, Cursor c, String[] from, int[] to) {
            super(context, layout, c, from, to);
            this.context = context;
            this.layout = layout;
        }

        @Override
        public View newView(Context context, Cursor cursor, ViewGroup parent) {

            Cursor c = getCursor();

            final LayoutInflater inflater = LayoutInflater.from(context);

            View v = inflater.inflate(layout, parent, false);

            int nameCol = c.getColumnIndex(db.KEY_ROUTE);
            String name = c.getString(nameCol);

            TextView name_text = (TextView) v.findViewById(R.id.name_entry);
            if (name_text != null) {
                name_text.setText(name);
            }

            return v;
        }

        @Override
        public void bindView(View v, Context context, Cursor c) {

            int nameCol = c.getColumnIndex(db.KEY_ROUTE);
            String name = c.getString(nameCol);

            TextView name_text = (TextView) v.findViewById(R.id.name_entry);

            if (name_text != null) {
                name_text.setText(name);
            }
        }

In my DBAdapter I have this:

public static final String TABLE_2= "route";

public static final String KEY_ROWID_2="_id";

public static final String KEY_ROUTE= "route";

public static final String KEY_USER_ID= "user_id";

This is how I query for the cursor:

public Cursor getAllData2() 
{
    return db.query(TABLE_2, new String[] {KEY_ROUTE},null,null,null,null,null);
}

and this is what my logcat gives me:

04-30 09:26:24.323: ERROR/AndroidRuntime(2676): Caused by: java.lang.IllegalArgumentException: column '_id' does not exist

04-30 09:26:24.323: ERROR/AndroidRuntime(2676): at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:314)

04-30 09:26:24.323: ERROR/AndroidRuntime(2676): at android.widget.CursorAdapter.init(CursorAdapter.java:111)

04-30 09:26:24.323: ERROR/AndroidRuntime(2676): at android.widget.CursorAdapter.(CursorAdapter.java:90)

04-30 09:26:24.323: ERROR/AndroidRuntime(2676): at android.widget.ResourceCursorAdapter.(ResourceCursorAdapter.java:47)

04-30 09:26:24.323: ERROR/AndroidRuntime(2676): at android.widget.SimpleCursorAdapter.(SimpleCursorAdapter.java:88)

04-30 09:26:24.323: ERROR/AndroidRuntime(2676): at com.server.Server8$ContactListCursorAdapter.(Server8.java:103)

04-30 09:26:24.323: ERROR/AndroidRuntime(2676): at com.server.Server8.onCreate(Server8.java:91)


Caused by: java.lang.IllegalArgumentException: column '_id' does not exist

I'm not even asking for something like this!!

and this is what my TABLE_2 looks like:

_id         route                  user_id

1           Sebes-Alba               1             ....only one record

What I'm doing wrong? Thanks

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

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

发布评论

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

评论(2

白日梦 2024-11-11 10:39:37

确保您的表有一个名为 _id 的列。常见的方法是将 _id 列创建为 _id INTEGER PRIMARY KEY AUTOINCRMENT
然后,确保每次查询数据库时始终查询 _id

参考文献:

有关列 ID 问题的详细信息

如何创建列

Make sure your table has a column named _id. The common approach is to create the _id column as _id INTEGER PRIMARY KEY AUTOINCREMENT.
Then, make sure you always query for the _id with each query to the database.

References:

Details on column ID issues

How to create a column

明媚殇 2024-11-11 10:39:37

您的日志显示您的查询目标(表)中没有 _id 列:

列“_id”不存在

您应该验证您的 route 表中是否有一列名为 _id,如果存在差异,您应该更新您的 public静态最终字符串 KEY_ROWID_2

Your log says that there is no _id column in the target of your query (table):

column '_id' does not exist

You should verify if you have a column in your route table named _id, and if there is difference, you should update your public static final String KEY_ROWID_2.

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