jeudi 29 octobre 2015

how to fix index out of bound exception : invalid index 0 size is 0 in android studio

here I am storing the response of yes no maybe into userrelation table. for that I have created table in DBCONTRACT and get the values in db helper. when I get the values and store into another variable it throws this error here I am posting the code

this the sql query for userRelation table

 public static abstract class RingeeUserRelationTable implements BaseColumns {

        public static final String TABLE_NAME = "user_relation";
        public static final String COL1_EVENT_USER_ID = "EVENT_USER_ID";
        public static final String COL2_EVENT_ID = "EVENT_ID";
        public static final String COL3_RINGEE_USER_ID = "RINGEE_USER_ID";
        public static final String COL4_IS_ATTENDING = "IS_ATTENDING";
        public static final String COL5_IS_DELETE = "IS_DELETE";

        public static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + "(" + _ID + " INTEGER PRIMARY KEY," + COL1_EVENT_USER_ID + INTEGER_TYPE + COMMA_SEP + COL2_EVENT_ID + INTEGER_TYPE
                + COMMA_SEP + COL3_RINGEE_USER_ID + INTEGER_TYPE + COMMA_SEP + COL4_IS_ATTENDING + INTEGER_TYPE + COMMA_SEP + COL5_IS_DELETE + INTEGER_TYPE + ")";

        public static final String DELETE_TABLE = "DROP TABLE IF EXISTS " + TABLE_NAME;

        public static final String RETRIVE_ALL_USER_DATA = "SELECT " + COL1_EVENT_USER_ID + COMMA_SEP + COL2_EVENT_ID + COMMA_SEP + COL3_RINGEE_USER_ID + COMMA_SEP + COL4_IS_ATTENDING + COMMA_SEP
                + COL5_IS_DELETE + " FROM " + TABLE_NAME;
    }

this is the code for getting the value and set to userMOS list

 public ArrayList<UserMO> getAllUserRelation() {
        ArrayList<UserMO> userMOs = new ArrayList<UserMO>();
        SQLiteDatabase db = this.getReadableDatabase();

        Cursor cursor = db.rawQuery(DatabaseContract.RingeeUserRelationTable.RETRIVE_ALL_USER_DATA, null );
        if (cursor.moveToFirst()) {
            do {
                UserMO userMO = new UserMO();
                userMO.setEventUserId(cursor.getLong(1));
                userMO.setEventId(cursor.getLong(2));
                userMO.setRingeeUserId(cursor.getLong(3));
                userMO.setIsAttending(cursor.getInt(4));
                userMO.setIsDelete(cursor.getInt(5));
            } while (cursor.moveToNext());

            cursor.close();
        }
        return userMOs;
    }

this is the code for getting the is attending value in fragment

context = getActivity().getApplicationContext();
        dbHelper = new DatabaseHelper(context);
        userMOs = dbHelper.getAllUserRelation();
int  isAttending = userMOs.get(position).getIsAttending(); 

I am using this isattending for setting the colour of yes no maybe button

  switch(isAttending)
            {
                case 1:
                    yesBtn.setBackgroundColor(Color.YELLOW);
                    noBtn.setBackgroundColor(Color.BLUE);
                    maybeBtn.setBackgroundColor(Color.BLUE);
                    break;
                case 2:
                    yesBtn.setBackgroundColor(Color.BLUE);
                    noBtn.setBackgroundColor(Color.BLUE);
                    maybeBtn.setBackgroundColor(Color.YELLOW);
                    break;
                case 0:
                    yesBtn.setBackgroundColor(Color.BLUE);
                    noBtn.setBackgroundColor(Color.YELLOW);
                    maybeBtn.setBackgroundColor(Color.BLUE);
                    break;


            }

when I run this project I got a error indexout of bound exception pls tell me what is the cause of the error and how to solve this issue

Aucun commentaire:

Enregistrer un commentaire