0

I try to implement the following code

In the activity.java file

      DatabaseEvent mDbHelper = new DatabaseEvent(getApplicationContext());
      mDbHelper.open();
      Cursor notesCursor = mDbHelper.fetchEvent();
             startManagingCursor(notesCursor);
        String[] from = new String[]{DatabaseEvent.KEY_ETITLE, DatabaseEvent.KEY_DISTANCE, DatabaseEvent.KEY_IMGNAME, DatabaseEvent.KEY_DESCRIPTION, DatabaseEvent.KEY_EID};
            int[] to = new int[]{R.id.title, R.id.duration, R.id.list_image, R.id.artist, R.id.id};
            SimpleCursorAdapter event = 
                    new SimpleCursorAdapter(getApplicationContext(), R.layout.list_row, notesCursor, from, to);

In the DatabaseEvent.java

               public long createEvent(String title, String distance, String imgname, String description, String eid) {
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_ETITLE, title);
        initialValues.put(KEY_DISTANCE, distance);
        initialValues.put(KEY_IMGNAME, imgname);
        initialValues.put(KEY_DESCRIPTION, description);
        initialValues.put(KEY_EID, eid);

          Log.v("INFO1","inserting db");
        return mDb.insert(EVENT_TABLE, null, initialValues);

    }


         public Cursor fetchEvent() {
         Log.v("INFO1","fetching db");
         Cursor mCursor = mDb.query(EVENT_TABLE, new String[] {KEY_ROWID, KEY_ETITLE, KEY_DISTANCE,
                KEY_IMGNAME, KEY_DESCRIPTION, KEY_EID}, null, null,null,null, KEY_DISTANCE+" ASC");

        return mCursor;


    }

On the logcat, I can clearly see that the log message "inserting db" is printed three times mean the data date really added to the database, but the log message "fetching db" printed once and give me the flowing error says:

             CursorIndexOutOfBoundException index -1, requsted, with a size of 60,

I tried different function like moveToFirst() and moveToNext() but still couldn't solve the problem, any one could give me hand, any help will be greately appreciated!

2
  • Please post all of your logcat errors. I don't think the error is where you think it is... Commented Jun 17, 2012 at 17:20
  • Of course the fetching part is only showing up once, you are only querying the DB once. Commented Jun 17, 2012 at 21:36

1 Answer 1

2

you forgot to include mCursor.moveToFirst();

Change your code to

   public Cursor fetchEvent() {
         Log.v("INFO1","fetching db");
         Cursor mCursor = mDb.query(EVENT_TABLE, new String[] {KEY_ROWID, KEY_ETITLE, KEY_DISTANCE,
                KEY_IMGNAME, KEY_DESCRIPTION, KEY_EID}, null, null,null,null, KEY_DISTANCE+" ASC");
        mCursor.moveToFirst();
        return mCursor;


    }
Sign up to request clarification or add additional context in comments.

3 Comments

While this is a smart move, if you are simply binding a Cursor to a ListView you do not move the Cursor from index -1.
Yes you are absolutely right! But CursorIndexOutOfBoundException index -1 is suggesting that his cursor is not pointing to first data.here is same post stackoverflow.com/questions/6710565/…
thnks for your answer, sam, but when I tried again, it gives me an error says" request failed, java lang null pointer exception"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.