简单的游标适配器问题 [英] Simple Cursor Adapter problem
问题描述
下面是我的code一个简单的游标适配器。
公共类CursorList扩展ListActivity {
/ **当第一次创建活动调用。 * /
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main); DatabaseAdapter DA =新DatabaseAdapter(这一点,mydb.sqlite); da.open();
CUR = da.fetchAllRecords光标(医生,新的String [] {名字});
startManagingCursor(现); cur.moveToFirst();
做{
Log.v(信息,cur.getString(cur.getColumnIndex(名字)));
}而(cur.moveToNext()); cur.moveToFirst(); 的String [] =由新的String [] {姓};
INT []为= INT新[] {} R.id.row; SimpleCursorAdapter SCA =新SimpleCursorAdapter(这一点,R.layout.row_item,CUR,从,到); setListAdapter(SCA);
}
}
的数据记录在日志中正确显示,但code停止工作时,它到达
SimpleCursorAdapter SCA =新SimpleCursorAdapter(这一点,R.layout.row_item,CUR,从,到);
行。
我得到的错误是:
ERROR / AndroidRuntime(26746):未捕获的处理程序:螺纹主力退出,由于未捕获的异常
ERROR / AndroidRuntime(26746):了java.lang.RuntimeException:无法启动活动ComponentInfo {com.arnab.cursorlist / com.arnab.cursorlist.CursorList}:
java.lang.IllegalArgumentException异常:列'_id'不存在
我在哪里去了?
为什么它给了列'_id'不存在错误?它是我们必须在我们的表的必要列?
编辑:
当我把光标相关code在try catch块,像这样:
{尝试
SimpleCursorAdapter SCA =新SimpleCursorAdapter(这一点,R.layout.row_item,CUR,从,到); setListAdapter(SCA);
}
赶上(例外五){
Log.v(错误,E.getMessage());
}
我得到的消息:
VERBOSE /错误(1026):列'_id'不存在
@Rasel:这里是 fetchAllRecords
法
公共光标fetchAllRecords(字符串表,字符串列[]){
返回mDb.query(表,列,NULL,NULL,NULL,NULL,NULL);
}
为什么它给了列'_id'不存在错误?它是我们必须在我们的表的必要列?
块引用>是的,如果你想用你的数据库信息,游标适配器。该适配器将其用于内部使用。你的表必须有一个_id列,并且必须在查询中选择它(所以它是在
光标
结果集)。您不必实际在ListView控件显示它
。修订为后人
而不是实际添加了_id列,可以
SELECT
你自己的'身份证'列'_id',它会工作一样的。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
methodpublic Cursor fetchAllRecords(String table, String columns[]) { return mDb.query(table, columns, null, null, null, null, null); }
解决方案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?
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.这篇关于简单的游标适配器问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!