我使用SQLite存储一些对象,然后在列表视图中显示。当活动启动时,我只需抓取数据库中的所有对象并使用结果填充列表视图。偶尔(大约5%的时间),我注意到并非所有结果都从db返回。我只对1个结果感到害羞,如果我重新加载活动,则缺少的结果会正确加载。
在尝试追踪问题时,我在查询后记录了Cursor对象计数,并注意到当结果丢失时,Cursor对象也缺少它(因此问题不在于对返回的对象进行膨胀)。有没有人有这个问题的经验?
下面是我在数据库中存储的对象示例。
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);
}
}
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);
}
}
现在有一个例子从我的数据库中加载对象。
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(); }
}
我知道,通读很多。希望有人之前已经看过这个,而且只是我在DataBaseHelper中做了一些事情。我感谢您提供的任何建议或建议。谢谢!
答案 0 :(得分:1)
尝试在此处使用以下代码我创建列表将此列表添加到列表视图中。
ArrayList results= new ArrayList();
SQLiteDatabase myDB = this.openOrCreateDatabase("dummy.db", SQLiteDatabase.OPEN_READWRITE, null);
try{
Cursor c = myDB.rawQuery("select a from xyz", null);
int Column1 = c.getColumnIndex("a");
// Check if our result was valid.
c.moveToFirst();
if (c != null) {
int i = 0;
// Loop through all Results
do {
i++;
String a= c.getString(Column1);
results.add(a);
} while (c.moveToNext());
}
} catch (SQLiteException e) {
e.printStackTrace();
} finally {
if (myDB != null)
myDB.close();
}