Search code examples
androidsqliteandroid-cursor

Cannot Retrieve autoincrement field from cursor


I have created a sqlite database.I am displaying name,image and description from the database and binding them in listview successfully.But I cannot retrieve the autoincrement field (cardid) from the table.

Here is code 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();
           }
         }
        }
});

And here is the stacktrace:

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)

code 2: Here i removed cursor.getColumnIndex("cardid") and inserted a columindex manually like:

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();
               }
             }
            }
    });

But it is not returning the id but returing a field called "cardcode".

Here is my sqlite data:

enter image description here

and here is the datastructure:

enter image description here

Can any one please point me the mistake I am doing??


Solution

  • Your Cursor does not contain the cardid column and getColumnIndex() returns -1. Check the projection you use when doing your query.