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!

smith
  • 5,341
  • 8
  • 31
  • 38
  • Please post all of your logcat errors. I don't think the error is where you think it is... – Sam Jun 17 '12 at 17:20
  • Of course the fetching part is only showing up once, you are only querying the DB once. – Barak Jun 17 '12 at 21:36

1 Answers1

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;


    }
Vipul
  • 27,808
  • 7
  • 60
  • 75
  • 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. – Sam Jun 17 '12 at 17:19
  • Yes you are absolutely right! But CursorIndexOutOfBoundException index -1 is suggesting that his cursor is not pointing to first data.here is same post http://stackoverflow.com/questions/6710565/images-in-simplecursoradapter – Vipul Jun 17 '12 at 17:21
  • thnks for your answer, sam, but when I tried again, it gives me an error says" request failed, java lang null pointer exception" – smith Jun 17 '12 at 18:02