I am trying to populate 10 text views with data from my HighscoreDB. Each time my application game ends, the user is asked to save his score in the database along with the name. Then I fill out 10 text views (actually 20, but there are 10 lines with a name and score). This worked (when there were several records in HighscoreDB), but since I created a new emulator and thereby deleted all the record lines, I continue to get the "android.database.CursorIndexOutOfBoundsException: requested index x with size x" errors. X represents the number of lines that I currently have, for example, if I have 5 record lines in the database, the error will be "a pointer 5 is requested with size 5" (it turned out that, having tried new ways to fix my problem, but it remained, and I noticed,that the index is also increasing). Can someone please indicate what I am doing wrong or how to say how to fix my code?
This is the code in my HighscoreActivity where records are placed in text fields:
db = (new HighscoreDB(this)).getWritableDatabase();
cursor = db.rawQuery("SELECT _id, name, score FROM highscore ORDER BY score DESC", null);
cursor.moveToFirst();
for (int i = 0; i < 10; i++) {
TableRow row = (TableRow)mHighscoreTable.getChildAt(i+1);
TextView tvName = (TextView)row.getChildAt(1);
tvName.setText(cursor.getString(1));
TextView tvScore = (TextView)row.getChildAt(2);
tvScore.setText(Integer.toString(cursor.getInt(2)));
cursor.moveToNext();
}
The line "for (int i = 0; i <10; i ++) {" is that I want to show only 10 records at a time in 10 lines of text views.
Here's how to make a table:
private static final String SCORE_TABLE_CREATE = "CREATE TABLE "
+ SCORE_TABLE_NAME
+ " (_id INTEGER PRIMARY KEY autoincrement, "
+ "name TEXT NOT NULL, score INTEGER NOT NULL)";
This was my last mistake:
05-25 17:03:16.786: E/AndroidRuntime(780): FATAL EXCEPTION: main
05-25 17:03:16.786: E/AndroidRuntime(780): java.lang.RuntimeException: Unable to start activity ComponentInfo{be.michiel.test/be.michiel.test.HighscoreActivity}: android.database.CursorIndexOutOfBoundsException: Index 5 requested, with a size of 5
05-25 17:03:16.786: E/AndroidRuntime(780): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
05-25 17:03:16.786: E/AndroidRuntime(780): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
05-25 17:03:16.786: E/AndroidRuntime(780): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-25 17:03:16.786: E/AndroidRuntime(780): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
05-25 17:03:16.786: E/AndroidRuntime(780): at android.os.Handler.dispatchMessage(Handler.java:99)
05-25 17:03:16.786: E/AndroidRuntime(780): at android.os.Looper.loop(Looper.java:123)
05-25 17:03:16.786: E/AndroidRuntime(780): at android.app.ActivityThread.main(ActivityThread.java:3683)
05-25 17:03:16.786: E/AndroidRuntime(780): at java.lang.reflect.Method.invokeNative(Native Method)
05-25 17:03:16.786: E/AndroidRuntime(780): at java.lang.reflect.Method.invoke(Method.java:507)
05-25 17:03:16.786: E/AndroidRuntime(780): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-25 17:03:16.786: E/AndroidRuntime(780): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-25 17:03:16.786: E/AndroidRuntime(780): at dalvik.system.NativeStart.main(Native Method)
05-25 17:03:16.786: E/AndroidRuntime(780): Caused by: android.database.CursorIndexOutOfBoundsException: Index 5 requested, with a size of 5
05-25 17:03:16.786: E/AndroidRuntime(780): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580)
05-25 17:03:16.786: E/AndroidRuntime(780): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214)
05-25 17:03:16.786: E/AndroidRuntime(780): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:41)
05-25 17:03:16.786: E/AndroidRuntime(780): at be.michiel.test.HighscoreActivity.onCreate(HighscoreActivity.java:79)
05-25 17:03:16.786: E/AndroidRuntime(780): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-25 17:03:16.786: E/AndroidRuntime(780): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
05-25 17:03:16.786: E/AndroidRuntime(780): ... 11 more
Please help me find my mistake. I tried to find out for a long time. Thanks in advance!