samedi 25 juillet 2015

Cursor skipping by 2 Android Sqlite

My cursor is skipping positions by 2. So it is skipping a row each time through. I think it is because I initialize a tempCursor = cursor and then moveToNext with the tempCursor but don't know why that would change the original cursor's position. How would I have another cursor check next row before the original cursor does?

Code:

    public ArrayList<String> getMeetsEventTimes(Athlete athlete){
    SQLiteDatabase db = getReadableDatabase();
    Cursor cursor = db.rawQuery("SELECT * FROM TABLE");

    if(cursor.moveToFirst()){
        do {
            int cursorPosition = cursor.getPosition();
            int meetID = cursor.getInt(cursor.getColumnIndex(COL_MEET_ID));
            int eventID = cursor.getInt(cursor.getColumnIndex(COL_EVENT_ID));

            if(firstEvent == true) {
                meetEventInfo = getMeetInfo(meetID, athlete.getAthleteID(), eventID);
                singleMeetEvents.add(meetEventInfo);
                firstEvent = false;
            }

            // Think this is the problem but not sure.
            // Trying to check next row before going to next row
            Cursor cursorTemp = cursor;
            if (cursorTemp.moveToNext()){
                int cursorTempPosition = cursorTemp.getPosition();
                int nextMeetID = cursorTemp.getInt(cursorTemp.getColumnIndex(COL_MEET_ID));
                int nextEventID = cursorTemp.getInt(cursorTemp.getColumnIndex(COL_EVENT_ID));
                // Next event is the same. Do nothing
                if(nextMeetID == meetID && nextEventID == eventID){
                    firstEvent = false;
                }else if(nextMeetID == meetID && nextEventID != eventID){
                    firstEvent = true;
                }else if(nextMeetID != meetID){
                    String meetEvents = "";
                    for(int i = 0; i < singleMeetEvents.size(); i++){
                        meetEvents = meetEvents + singleMeetEvents.get(i).toString() + '\n';
                    }
                    meetEventTimes.add(meetEvents);
                }
            }else{
                meetEventTimes.add(meetEventInfo);
            }
        }while(cursor.moveToNext());
    }
    cursor.close();
    return meetEventTimes;
}

Aucun commentaire:

Enregistrer un commentaire