Android : diificulty in getting cursor data and store it in a string

on Sunday, August 3, 2014


Hey i m learning android, and having some trouble in sqLite database.. I want to get a specific set of rows from a column through cursor and store it in string array to display it in textview. this is the method i m using to get the cursor data



public String[] getDetailedSymps(long sympId) {

String[] columns2 = new String[] { KEY_SYMP_DETAIL, KEY_SYM_ID };
String details[]= new String[9];
int i=0;


Cursor c3 = ourDatabase.query(DATABASE_TABLE2, columns2, KEY_SYM_ID + "=" + sympId, null, null, null, null);
int iDetail = c3.getColumnIndex(KEY_SYMP_DETAIL);
while (c3.isAfterLast() == false) {

details[i]= c3.getString(iDetail);
i++;
c3.moveToNext();
}

return details;


But ,when i ran the code , it gave me the error CursorIndexOutOfBoundException,: index -1 requested with a size of 9, here is the logcat



08-03 03:22:06.294: W/dalvikvm(1194): threadid=1: thread exiting with uncaught exception (group=0xb4ae2ba8)
08-03 03:22:06.464: E/AndroidRuntime(1194): FATAL EXCEPTION: main
08-03 03:22:06.464: E/AndroidRuntime(1194): Process: com.example.mydb, PID: 1194
08-03 03:22:06.464: E/AndroidRuntime(1194): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mydb/com.example.mydb.DetailActivity}: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 9
08-03 03:22:06.464: E/AndroidRuntime(1194): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
08-03 03:22:06.464: E/AndroidRuntime(1194): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
08-03 03:22:06.464: E/AndroidRuntime(1194): at android.app.ActivityThread.access$800(ActivityThread.java:135)
08-03 03:22:06.464: E/AndroidRuntime(1194): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
08-03 03:22:06.464: E/AndroidRuntime(1194): at android.os.Handler.dispatchMessage(Handler.java:102)
08-03 03:22:06.464: E/AndroidRuntime(1194): at android.os.Looper.loop(Looper.java:136)
08-03 03:22:06.464: E/AndroidRuntime(1194): at android.app.ActivityThread.main(ActivityThread.java:5017)
08-03 03:22:06.464: E/AndroidRuntime(1194): at java.lang.reflect.Method.invokeNative(Native Method)
08-03 03:22:06.464: E/AndroidRuntime(1194): at java.lang.reflect.Method.invoke(Method.java:515)
08-03 03:22:06.464: E/AndroidRuntime(1194): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
08-03 03:22:06.464: E/AndroidRuntime(1194): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
08-03 03:22:06.464: E/AndroidRuntime(1194): at dalvik.system.NativeStart.main(Native Method)
08-03 03:22:06.464: E/AndroidRuntime(1194): Caused by: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 9
08-03 03:22:06.464: E/AndroidRuntime(1194): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:426)
08-03 03:22:06.464: E/AndroidRuntime(1194): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
08-03 03:22:06.464: E/AndroidRuntime(1194): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
08-03 03:22:06.464: E/AndroidRuntime(1194): at com.example.mydb.Dbadapter.getDetailedSymps(Dbadapter.java:296)
08-03 03:22:06.464: E/AndroidRuntime(1194): at com.example.mydb.DetailActivity.onCreate(DetailActivity.java:25)
08-03 03:22:06.464: E/AndroidRuntime(1194): at android.app.Activity.performCreate(Activity.java:5231)
08-03 03:22:06.464: E/AndroidRuntime(1194): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-03 03:22:06.464: E/AndroidRuntime(1194): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)


Can anyone help, what am i doing wrong here, Thanks in advance.


0 comments:

Post a Comment