我创建了一个sqlite数据库。我正在显示数据库中的名称,图像和描述,并成功地将它们绑定在listview中。但是我无法从表中检索自动增量字段(cardid)。 / p>
以下是代码1:
lvcard.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
// TODO Auto-generated method stub
if(cursor!=null){
if(cursor.moveToFirst()){
cursor.moveToPosition(position);
String cardid = ""+cursor.getInt(cursor.getColumnIndex("cardid"));
Toast. makeText(CardList.this,"cardid--->"+cardid,Toast.LENGTH_LONG).show();
}
}
}
});
这是堆栈跟踪:
11-06 15:01:54.579: E/AndroidRuntime(20071): FATAL EXCEPTION: main
11-06 15:01:54.579: E/AndroidRuntime(20071): java.lang.IllegalStateException: get field slot from row 3 col -1 failed
11-06 15:01:54.579: E/AndroidRuntime(20071): at android.database.CursorWindow.getLong_native(Native Method)
11-06 15:01:54.579: E/AndroidRuntime(20071): at android.database.CursorWindow.getInt(CursorWindow.java:465)
11-06 15:01:54.579: E/AndroidRuntime(20071): at android.database.AbstractWindowedCursor.getInt(AbstractWindowedCursor.java:124)
11-06 15:01:54.579: E/AndroidRuntime(20071): at com.webguru.india.ctp.CardList$1.onItemClick(CardList.java:64)
11-06 15:01:54.579: E/AndroidRuntime(20071): at android.widget.AdapterView.performItemClick(AdapterView.java:315)
11-06 15:01:54.579: E/AndroidRuntime(20071): at android.widget.ListView.performItemClick(ListView.java:3570)
11-06 15:01:54.579: E/AndroidRuntime(20071): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1862)
11-06 15:01:54.579: E/AndroidRuntime(20071): at android.os.Handler.handleCallback(Handler.java:618)
11-06 15:01:54.579: E/AndroidRuntime(20071): at android.os.Handler.dispatchMessage(Handler.java:123)
11-06 15:01:54.579: E/AndroidRuntime(20071): at android.os.Looper.loop(SourceFile:351)
11-06 15:01:54.579: E/AndroidRuntime(20071): at android.app.ActivityThread.main(ActivityThread.java:3826)
11-06 15:01:54.579: E/AndroidRuntime(20071): at java.lang.reflect.Method.invokeNative(Native Method)
11-06 15:01:54.579: E/AndroidRuntime(20071): at java.lang.reflect.Method.invoke(Method.java:538)
11-06 15:01:54.579: E/AndroidRuntime(20071): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:969)
11-06 15:01:54.579: E/AndroidRuntime(20071): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:727)
11-06 15:01:54.579: E/AndroidRuntime(20071): at dalvik.system.NativeStart.main(Native Method)
代码2: 在这里我删除了cursor.getColumnIndex(&#34; cardid&#34;)和手动插入columindex ,如:
lvcard.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
// TODO Auto-generated method stub
if(cursor!=null){
if(cursor.moveToFirst()){
cursor.moveToPosition(position);
String cardid = ""+cursor.getInt(3);
Toast. makeText(CardList.this,"cardid--->"+cardid,Toast.LENGTH_LONG).show();
}
}
}
});
但它没有返回id ,而是撤回名为&#34; cardcode&#34;的字段。
这是我的 sqlite数据:
这是数据结构:
任何人都可以指出我正在做的错误吗?
答案 0 :(得分:0)
您的Cursor
不包含cardid
列,getColumnIndex()
返回-1。检查查询时使用的投影。