我将yes|no|maybe
的回复存储到userrelation
表中
为此,我在DBCONTRACT
中创建了一个表,并在db helper
类中获取值。
当我获取值并存储到另一个变量时,它会抛出此错误
这是userRelation
表
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;
}
这是获取值并设置为userMOS
列表
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));
userMOs.add(userMO);
} while (cursor.moveToNext());
cursor.close();
}
return userMOs;
}
这是在片段中获取isattending
值的代码
context = getActivity().getApplicationContext();
dbHelper = new DatabaseHelper(context);
userMOs = dbHelper.getAllUserRelation();
// I got the error here there is no value in a table but in back end the values are stored
int isAttending = userMOs.get(position).getIsAttending();
我正在使用此isattending
来设置yes|no|maybe
按钮
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;
}
当我运行这个项目时,我收到一个错误:IndexOutOfBoundsException 请告诉我错误的原因是什么以及如何解决这个问题
答案 0 :(得分:1)
索引 0基于 因此,您需要像这样重写代码:
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(0));
userMO.setEventId(cursor.getLong(1));
userMO.setRingeeUserId(cursor.getLong(2));
userMO.setIsAttending(cursor.getInt(3));
userMO.setIsDelete(cursor.getInt(4));
userMOs.add(userMO);
} while (cursor.moveToNext());
cursor.close();
}
return userMOs;
}
答案 1 :(得分:0)
编写如下代码: -
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));
userMos.add(userMO);
} while (cursor.moveToNext());
cursor.close();
}
return userMOs;
}
它为您提供了一个超出绑定异常的数组索引,因为您没有为数组列表指定任何内容。我在上面的代码中添加了这一行。
userMos.add(userMo);