dimanche 25 octobre 2015

Android SQLite cursore move to previous not working

I am trying to make a simple sqlite program in android that will move my cursor next and previous and give the values to me. The problem is that moveToNext is working fine but moveToPrevious is not working Please help

case R.id.BUTTON_NEXT:
                cursorNext = dbHelper.retrieveNext();
                if(cursorNext!=null){
                if (cursorNext.moveToNext()) {

                    NAME.setText(cursorNext.getString(1));
                    ROLLNO.setText(cursorNext.getString(2));
                    CLASS.setText(cursorNext.getString(3));
                    PHONE.setText(cursorNext.getString(4));
                }


                }


                break;
            case R.id.BUTTON_PREVIOUS:

                cursorPrev = dbHelper.retrievePrev();


                if (cursorPrev.moveToPrevious())
            {

                    NAME.setText(cursorPrev.getString(1));
                    ROLLNO.setText(cursorPrev.getString(2));
                    CLASS.setText(cursorPrev.getString(3));
                    PHONE.setText(cursorPrev.getString(4));


                }

                break;
        }

DBHelper.class

int count = 1;
public Cursor retrieveNext() {
        Cursor Next = null;
        SQLiteDatabase database = this.getReadableDatabase();
        Cursor cursor1 = database.rawQuery("SELECT * FROM " + TABLE_NAME, null);
        rowCount = cursor1.getCount();

        if (count > rowCount) {
            Toast.makeText(context, "This was the last record", Toast.LENGTH_SHORT).show();
        } else if(count <= rowCount) {

            Next = database.rawQuery("SELECT * FROM " + TABLE_NAME + " WHERE ID= " + count, null);
            Toast.makeText(context, String.valueOf(count), Toast.LENGTH_SHORT).show();
            count++;

        }

        return Next;

    }

    public Cursor retrievePrev() {
        SQLiteDatabase sqLiteDatabase = this.getReadableDatabase();

        Cursor PREV = null;
        if (count < 1) {
            Toast.makeText(context, "This was the Very First record", Toast.LENGTH_SHORT).show();
            count = 1;
        } else if(count >=1){
            count--;
            PREV = sqLiteDatabase.rawQuery("SELECT * FROM " + TABLE_NAME + " WHERE ID=" + count, null);
            Toast.makeText(context, String.valueOf(count), Toast.LENGTH_SHORT).show();


        }


        return PREV;

    }

Aucun commentaire:

Enregistrer un commentaire