通常,此代码非常有效。但是,如果cursor
为空,则main.class
中会出错。我尝试了很多东西。但是,我没有取得成功。请帮忙解决这个问题。
---- database.class ---
public List<Integer> count_a() {
List<Integer> list = new ArrayList<Integer>();
String selectQuery = "select book, count(date) from myTAB WHERE (date>'" + 0 + "') group by book";
// if result is empty, there is an error in main.class.
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
list.add(Integer.parseInt(cursor.getString(1)));
} while (cursor.moveToNext());
}
cursor.close();
db.close();
return list;
}
---- main.class ----
private void load_data() {
Database db = new Database(getApplicationContext());
List<Integer> co_a = db.count_a(); // error is here
Integer[] co_b = new Integer[co_a.size()];
co_b = co_a.toArray(co_b);
List_Row adapter = new List_Row(this, co_b);
ListView lv = (ListView) findViewById(R.id.lvStat);
lv.setAdapter(adapter);
}