我的代码是:
DatabaseHandler db = new DatabaseHandler(this);
System.out.println("Start - " + System.currentTimeMillis());
for (int i = 1; i < db.getChampsCount() + 1; i++) {
String name = db.getChampInfo(i, "name");
String title = db.getChampInfo(i, "title");
String thumb = db.getChampInfo(i, "thumb");
System.out.println("End - " + System.currentTimeMillis());
[...]
}
和这个
String getChampInfo(int id, String col) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT " + col + " FROM champions WHERE id = " + id, new String[] {});
if (cursor != null)
cursor.moveToFirst();
db.close();
return cursor.getString(0);
}
这是DatabaseHelper类的一部分
这个运行没有问题,但是问题是它执行起来时间很长(在我的android机子上是2089ms)。这些字符串是UI的一部分,所以我不认为我能把他放到另外一个线程中。如果要让这段代码跑的快一点,我应该怎么做?
确切的说是110行