我如何使用,如果SQLite中的游标为空?

时间:2016-12-06 13:22:55

标签: android sqlite cursor android-sqlite is-empty

通常,此代码非常有效。但是,如果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);
}

0 个答案:

没有答案