我在从数据库收集数据的活动中有此代码。活动开始需要 5 秒。没有这个,活动需要 2 秒才能打开。有没有办法优化这段代码?
所有数组(包括循环中的数组:arr_calllog_name0)在我的手机上有 61 个项目。在模拟器中它很快,有 6 个项目。
for (int i=0; i<arr_calllog_name0.size(); i++)
{
Cursor crname = info.getAllTitles_Stats2A(arr_calllog_name0.get(i), "1", d1, d2);
crname.moveToFirst();
count_in = crname.getInt(0);
arr_calllog_numberin0.add(String.valueOf(count_in));
Cursor crname2 = info.getAllTitles_Stats2A(arr_calllog_name0.get(i), "2", d1, d2);
crname2.moveToFirst();
count_out = crname2.getInt(0);
arr_calllog_numberout0.add(String.valueOf(count_out));
Cursor crname3 = info.sumAllTitles_StatsA(arr_calllog_name0.get(i), "1", d1, d2);
crname3.moveToFirst();
sum_in = crname3.getInt(0);
arr_calllog_durationin0.add(String.valueOf(sum_in));
Cursor crname4 = info.sumAllTitles_StatsA(arr_calllog_name0.get(i), "2", d1, d2);
crname4.moveToFirst();
sum_out = crname4.getInt(0);
arr_calllog_durationout0.add(String.valueOf(sum_out));
}
info 被声明为HotOrNot info = new HotOrNot(ChartsDuration.this);
其中 HotOrNot 是包含 DbHelper 的类扩展 SQLiteOpenHelper 类。
一个查询是(另一个几乎相同)
public Cursor getAllTitles_Stats2A(String name, String type, String date1, String date2) {
return ourDatabase.rawQuery("SELECT COUNT(*) FROM " + DATABASE_TABLE + " WHERE " + KEY_DATE + " BETWEEN '" + date1 + "'" + " AND '" + date2 + "'" + " AND " + KEY_NAME + " = '" + name + "'" + " AND " + KEY_TYPE + " = '" + type + "'" + " COLLATE NOCASE", null);
}
DATABASE_TABLE 在我的手机上有 500 个项目。