好的,所以我正在尝试ArrayList
从我的 SQLite DB 创建一个小而轻量级的。
这是代码:
public ArrayList<Album> getAlbums()
{
Cursor cursor = null;
Cursor cursor2 = null;
try{
Log.d(Global.TAG, "STARTING");
ArrayList<Album> albums = new ArrayList<Album>();
Album album = new Album();
db = getOpenDatabase(DBADS);
String albumQuery = "SELECT * FROM " +albumsTableName + " ORDER BY " +albumIsMain + " DESC";
cursor = db.rawQuery(albumQuery, null);
if (cursor.moveToFirst()){
do {
Log.d(Global.TAG, "IN LOOP");
album = new Album();
album.id = cursor.getInt(0);
album.albumName = cursor.getString(1);
album.passcode = cursor.getString(2);
album.isMainAlbum = cursor.getInt(3) == 1 ? true : false;
albumQuery = "SELECT COUNT(*) FROM " + picturesTableName+" WHERE "+ pictureAlbumId +"="+album.id;
cursor2 = db.rawQuery(albumQuery, null);
cursor2.moveToFirst();
album.photosCount = cursor2.getInt(0);
cursor2.close();
albums.add(album);
}
while (cursor.moveToNext());
}
Log.d(Global.TAG, "RETURNING");
return albums;
}
catch(Exception ex)
{
Toast.makeText(context, ex.toString(), Toast.LENGTH_LONG).show();
}
finally
{
if(cursor != null){
cursor.close();
}
if(cursor2 != null){
cursor2.close();
}
if(cursor3 != null){
cursor3.close();
}
}
return null;
}
以下代码在 8 秒内执行,总共只有 6 个项目!这只是非常缓慢。
这里没有1000行,只有6行。经过一些调试,我看到如果我删除另一个第二个内部游标,它变得非常快。
这可能是什么原因?
非常感谢。