简单的游标适配器问题
这是我的简单光标适配器的代码。
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);
}
Here is my code for a simple cursor adapter.
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);
}
}
The data records are displayed correctly in the log, but the code stops working when it reaches the
SimpleCursorAdapter sca = new SimpleCursorAdapter(this, R.layout.row_item, cur, from, to);
line.
The error I get is :
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
Where am I going wrong?
Why does it give an error that column '_id' doesn't exist? Is it a necessary column which we have to have in our tables?
EDIT:
When I put the cursor related code in a try catch block, something like this:
try {
SimpleCursorAdapter sca = new SimpleCursorAdapter(this, R.layout.row_item, cur, from, to);
setListAdapter(sca);
}
catch(Exception E) {
Log.v("Error", E.getMessage());
}
I get the message :
VERBOSE/Error(1026): column '_id' does not exist
@Rasel : Here is the fetchAllRecords
method
public Cursor fetchAllRecords(String table, String columns[]) {
return mDb.query(table, columns, null, null, null, null, null);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,如果您想在游标适配器中使用数据库信息。适配器将其用于内部目的。您的表必须有一个“_id”列,并且您必须在查询中选择它(因此它位于
Cursor
结果集中)。您不必在ListView
中实际显示它。为后代进行修订
您可以
SELECT
您自己的“id”列作为“_id”,而不是实际添加“_id”列,并且它的工作方式是一样的。Yes, if you want to use your database information in a cursor adapter. The adapter uses it for internal purposes. Your table must have an '_id' column, and you must select it in your query (so it is in the
Cursor
result set). You do not have to actually display it in yourListView
.Revised for Posterity
Instead of actually adding the '_id' column, you can
SELECT
your own 'id' column as '_id', and it will work just the same.在 try catch 块中编写与游标相关的代码。您的问题将得到解决。检查创建表时您键入了不同的列而不是“_id”
Write Cursor related code in try catch block.Your problem will be solved.Check when created table you typed different column rather than '_id'
您需要在投影中包含表 _id。列表游标适配器需要 _id 来跟踪行。您不必在任何地方实际显示它或使用它,但光标需要包含该列。每个 Android 表中都必须有一个名为 _id 的主键列。
You need to include the table _id in the projection. the list cursor adapter needs the _id to keep track of the rows. you don't have to actually display it anywhere or use it but the cursor needs to contain that column. also a primary key column named _id is mandatory in every android table.