I am using SQLite to store some objects, which are then displayed in a list view. When the activity launches, I simply grab all objects in the db and populate the list view with the results. Occasionally (around 5% of the time), I notice that not all results have returned from the db. I have only ever been shy by 1 result, and, if I reload the activity, the missing result loads properly.
In trying to track down the problem, I logged the Cursor object count post-query and noticed that when a result was missing, it was also missing from the Cursor object (so the problem is not in inflating the returned objects). Does anyone have any experience with this problem?
Below is an example of an object I am storing in my db.
public class MyObject {
public static final String DB_TABLE = "object";
public static final int DB_VERSION = 1;
public static final String COL_ID = "id";
public static final String COL_TITLE = "title";
public static final String COL_SUBTITLE = "subtitle";
public static final String COL_DESCRIPTION = "description";
public static final String COL_START_TIME = "start_time";
public static final String COL_END_TIME = "end_time";
public static final String COL_IMAGE1 = "background_image";
public static final String COL_IMAGE2 = "icon_image";
public static final String COL_EXTRAS = "extras";
public static final String DB_CREATE = String.format("create table if not exists %1$s (%2$s text primary key, %3$s text, %4$s text, %5$s text, %6$s integer, %7$s integer, %8$s text, %9$s text, %10$s integer);",
DB_TABLE, COL_ID, COL_TITLE, COL_SUBTITLE, COL_DESCRIPTION, COL_START_TIME, COL_END_TIME, COL_IMAGE1, COL_IMAGE2, COL_EXTRAS);
public static final String [] DB_COLUMNS = new String [] {COL_ID, COL_TITLE, COL_SUBTITLE, COL_DESCRIPTION, COL_START_TIME, COL_END_TIME, COL_IMAGE1, COL_IMAGE2, COL_EXTRAS};
public String Id;
public String Title;
public String Subtitle;
public String Description;
public long StartTime;
public long EndTime;
public String Image2;
public String Image2;
public int Extras;
public Event() {}
public static DataBaseHelper getDBHelper(Context context) {
return new DataBaseHelper(context, QmobixConferenceApplication.DB_NAME, DB_VERSION, DB_CREATE, DB_TABLE, COL_ID);
}
}
And the relevant chunks of DataBaseHelper…
public class DataBaseHelper extends SQLiteOpenHelper {
private static final String TAG = "DataBaseHelper";
private SQLiteDatabase mDB;
private String mSQL;
private String mDBName;
private String mTableName;
private String mKeyId;
private boolean isClosed = false;
public DataBaseHelper(Context context, String DBName, int version, String sql, String tableName, String keyId) throws SQLiteException {
super(context, DBName, null, version);
this.mSQL = sql;
this.mDBName = DBName;
this.mTableName = tableName;
this.mKeyId = keyId;
try {
mDB = this.getWritableDatabase();
mDB.execSQL(mSQL);
} catch(SQLiteException ex) {
Log.e(TAG, String.format("Could not create and/or open the database [ %1$s ]", mDBName), ex);
throw ex;
}
}
@Override
public void onCreate(SQLiteDatabase db) {
try {
db.execSQL(mSQL);
} catch(SQLException ex) {
Log.e(TAG, String.format("Could not create table according to SQL [ %1$s }", mSQL), ex);
}
}
@Override
public synchronized void close() {
super.close();
if(mDB != null) {
mDB.close();
}
isClosed = true;
}
public Cursor get(String [] columns, String orderBy) {
return mDB.query(mTableName, columns, null, null, null, null, orderBy);
}
}
And now for an example loading objects from my db.
DataBaseHelper db = null;
Cursor cursor = null;
try {
db = MyObject.getDBHelper(mContext);
cursor = db.get(MyObject.DB_COLUMNS, String.format("%1$s ASC", MyObject.COL_START_TIME));
} catch(Exception ex) {
// bad news
} finally {
if(cursor != null) { cursor.close(); }
if(db != null) { db.close(); }
}
It’s a lot to read through, I know. Hopefully someone has seen this before, and it’s just that I’m doing things a bit off in my DataBaseHelper. I appreciate any suggestions or advice you can offer. Thanks!
Try to use below code here i am creating list add this list to your listview.