简单光标适配器问题

问题描述 投票:1回答:4

这是我的简单游标适配器的代码。

public class CursorList extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    DatabaseAdapter da = new DatabaseAdapter(this, "mydb.sqlite");

    da.open();
    Cursor cur = da.fetchAllRecords("Doctors", new String[]{"FirstName"});
    startManagingCursor(cur);

    cur.moveToFirst();
    do {
        Log.v("Info", cur.getString(cur.getColumnIndex("FirstName")));
    } while(cur.moveToNext());

    cur.moveToFirst();

    String[] from = new String[]{"FirstName"};
    int[] to = new int[]{R.id.row};

    SimpleCursorAdapter sca = new SimpleCursorAdapter(this, R.layout.row_item, cur, from, to);

    setListAdapter(sca);
    }
}

数据记录在日志中正确显示,但是代码到达]时停止工作。>

SimpleCursorAdapter sca = new SimpleCursorAdapter(this, R.layout.row_item, cur, from, to);

行。

我得到的错误是:

ERROR/AndroidRuntime(26746): Uncaught handler: thread main exiting due to uncaught exception
ERROR/AndroidRuntime(26746): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.arnab.cursorlist/com.arnab.cursorlist.CursorList}:
java.lang.IllegalArgumentException: column '_id' does not exist

我在哪里出错?

为什么会给出错误信息,表示列“ _id”不存在?这是我们表中必须包含的必要列吗?

编辑:

当我将与光标相关的代码放在try catch块中时,如下所示:

try {
        SimpleCursorAdapter sca = new SimpleCursorAdapter(this, R.layout.row_item, cur, from, to);

        setListAdapter(sca);
    }
    catch(Exception E) {
        Log.v("Error", E.getMessage());
    }

我收到消息:

VERBOSE/Error(1026): column '_id' does not exist

@@ Rasel:这是fetchAllRecords方法

public Cursor fetchAllRecords(String table, String columns[]) {
    return mDb.query(table, columns, null, null, null, null, null);     
}

这是我的简单光标适配器代码。公共类CursorList扩展ListActivity {/ **在首次创建活动时调用。 * / @Override public void onCreate(Bundle savedInstanceState)...

android listview android-widget simplecursoradapter
4个回答
2
投票

为什么会给出错误信息,表示列“ _id”不存在?这是我们表中必须包含的必要列吗?


0
投票

在try catch块中编写与游标相关的代码。您的问题将得到解决。检查在创建表时键入了不同的列而不是'_id'


0
投票

您需要在投影中包含表_id。列表游标适配器需要_id来跟踪行。您不必实际在任何地方显示它或使用它,但光标需要包含该列。在每个android表中,名为_id的主键列也是必需的。


0
投票

有时_id问题,我介意显示您创建表的sql代码

© www.soinside.com 2019 - 2024. All rights reserved.