I am making a listing of the information contained in my SQLite database
However, sometimes I get the following error:
java.lang.ArrayIndexOutOfBoundsException: length = 13; index = 1401
The intriguing that this only occurs from time to time, and if it gives error and then I redo the query the error disappears.
Has anyone ever had something similar and / or have you any idea?
public class AgendaDatabaseHelper extends SQLiteOpenHelper {
// Database Info
private static final String DATABASE_NAME = "agenda";
private static final int DATABASE_VERSION = 1;
// Table Name
private static final String TABLE_NAME = "agenda";
// Table Columns
private static final String ID = "_id";
private static final String DATA = "data";
private static final String HORA = "hora";
private static final String ID_USER = "iduser";
private static final String NOME = "nome";
private static final String DATA_AGENDAMENTO = "dataagendamento";
private static final String OBSERVACOES = "observacoes";
private static AgendaDatabaseHelper sInstance;
Locale localeBR = new Locale("pt", "BR");
SimpleDateFormat fmtDateBr = new SimpleDateFormat("dd/MM/yyyy",localeBR);
SimpleDateFormat fmtDateSQL = new SimpleDateFormat("yyyy-MM-dd",localeBR);
public AgendaDatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onConfigure(SQLiteDatabase db) {
super.onConfigure(db);
db.setForeignKeyConstraintsEnabled(true);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_AGENDA_TABLE = "CREATE TABLE " + TABLE_NAME +"(" +
ID + " INTEGER PRIMARY KEY," +
DATA + " DATE," +
HORA + " TIME," +
ID_USER + " INTEGER," +
DATA_AGENDAMENTO + " DATE," +
NOME + " TEXT," +
OBSERVACOES + " TEXT" +
")";
db.execSQL(CREATE_AGENDA_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (oldVersion != newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME + ";");
onCreate(db);
}
}
public static synchronized AgendaDatabaseHelper getInstance(Context context) {
if (sInstance == null) {
sInstance = new AgendaDatabaseHelper(context.getApplicationContext());
}else{
}
return sInstance;
}
public void insertAgenda(JSONObject item_Agenda) {
SQLiteDatabase db = getWritableDatabase();
db.beginTransaction();
try {
ContentValues values = new ContentValues();
values.put(ID, item_Agenda.getInt("ID"));
values.put(ID_USER, item_Agenda.getInt("IDUSER"));
if (item_Agenda.getString("DATA") != null && !item_Agenda.getString("DATA").isEmpty()) {
values.put(DATA, DateSQL(item_Agenda.getString("DATA")));
}
values.put(HORA, item_Agenda.getString("HORARIO"));
values.put(NOME, item_Agenda.getString("NOME"));
if (item_Agenda.getString("DATAAGENDAMENTO") != null && !item_Agenda.getString("DATAAGENDAMENTO").isEmpty()) {
values.put(DATA_AGENDAMENTO, DateSQL(item_Agenda.getString("DATAAGENDAMENTO")) );
}
values.put(OBSERVACOES, item_Agenda.getString("OBSERVACOES"));
db.insertOrThrow(TABLE_NAME, null, values);
db.setTransactionSuccessful();
} catch (Exception e) {
Log.d("TAG42", String.valueOf(e));
} finally {
db.endTransaction();
}
}
public List<Agenda> selectAgenda(String data, int idUser) {
List<Agenda> agendaRegistros = new ArrayList<>();
String POSTS_SELECT_QUERY = String.format(
"SELECT * FROM %s WHERE %s = '%s' AND %s = %s ORDER BY %s ASC",
TABLE_NAME, DATA, DateSQL(data), ID_USER, idUser, HORA);
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.rawQuery(POSTS_SELECT_QUERY, null);
try {
if (cursor.moveToFirst()) {
do {
Agenda agendaRegistro = new Agenda();
agendaRegistro.setAgData(String.valueOf(fmtDateBr.format(fmtDateSQL.parse(cursor.getString(cursor.getColumnIndex(DATA))))));
agendaRegistro.setAgHora(String.valueOf(cursor.getString(cursor.getColumnIndex(HORA))).substring(0,5));
String datAgendamento = cursor.getString(cursor.getColumnIndex(DATA_AGENDAMENTO));
if ( datAgendamento != null && !datAgendamento.isEmpty()) {
agendaRegistro.setAgDataAgendamento(String.valueOf(fmtDateBr.format(fmtDateSQL.parse(datAgendamento))));
}else{
}
agendaRegistro.setAgNome(cursor.getString(cursor.getColumnIndex(NOME)));
agendaRegistro.setAgObservacoes(cursor.getString(cursor.getColumnIndex(OBSERVACOES)));
agendaRegistros.add(agendaRegistro);
} while(cursor.moveToNext());
}
} catch (Exception e) {
Log.d("TAG42", String.valueOf(e));
cursor.close();
} finally {
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
}
return agendaRegistros;
}
public void deleteAgendaData(String data, int idUser) {
SQLiteDatabase db = getWritableDatabase();
db.beginTransaction();
try {
db.delete(TABLE_NAME, DATA + "=? AND " + ID_USER + "=?",
new String[] {
DateSQL(data),
String.valueOf(idUser)
});
db.setTransactionSuccessful();
} catch (Exception e) {
Log.d("TAG42", String.valueOf(e));
} finally {
db.endTransaction();
}
}
public void deleteAgendaUser(int idUser) {
SQLiteDatabase db = getWritableDatabase();
db.beginTransaction();
try {
db.delete(TABLE_NAME, ID_USER + "='" +idUser+"'", null);
db.setTransactionSuccessful();
} catch (Exception e) {
Log.d("TAG42", String.valueOf(e));
} finally {
db.endTransaction();
}
}
public static String DateBR(String DateSQL){
String[] dataSplit = DateSQL.split("-");
return dataSplit[2] + "/" + dataSplit[1] + "/" + dataSplit[0];
}
public static String DateSQL(String DateBR){
String[] dataSplit = DateBR.split("/");
return dataSplit[2] + "-" + dataSplit[1] + "-" + dataSplit[0];
}
}
Calendar class
public class Agenda {
private String _id;
private String agData;
private String agHora;
private String agNome;
private String agDataAgendamento;
private String agObservacoes;
public String get_id() {
return _id;
}
public void set_id(String _id) {
this._id = _id;
}
public String getAgData() {
return agData;
}
public void setAgData(String agData) {
this.agData = agData;
}
public String getAgHora() {
return agHora;
}
public void setAgHora(String agHora) {
this.agHora = agHora;
}
public String getAgNome() {
return agNome;
}
public void setAgNome(String agNome) {
this.agNome = agNome;
}
public String getAgDataAgendamento() {
return agDataAgendamento;
}
public void setAgDataAgendamento(String agDataAgendamento) {
this.agDataAgendamento = agDataAgendamento;
}
public String getAgObservacoes() {
return agObservacoes;
}
public void setAgObservacoes(String agObservacoes) {
this.agObservacoes = agObservacoes;
}
@Override
public String toString() {
return "Agenda{" +
"_id='" + _id + '\'' +
", agData='" + agData + '\'' +
", agHora='" + agHora + '\'' +
", agNome='" + agNome + '\'' +
", agDataAgendamento='" + agDataAgendamento + '\'' +
", agObservacoes='" + agObservacoes + '\'' +
'}';
}
}
Thanks for your attention.