Normaly, this code working very well. But, if "cursor" is empty, there is an error in main.class. I tried a lot of thing. But, I didn't success. Please help to solve way.
---- 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";
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();
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);
}
What I have tried:
Failure 1
if (cursor.moveToFirst()) {
do {
if (cursor.getCount() == 0 || cursor == null) {
list.add(0);
} else {
list.add(Integer.parseInt(cursor.getString(1)));
}
}
while (cursor.moveToNext());
}
Failure 2
if (cursor.moveToFirst()) {
do {
if(cursor.getCount() == 0){
list.add(0);
}else {
list.add(Integer.parseInt(cursor.getString(1)));
}
}
while (cursor.moveToNext());
}
Failure 3
if (cursor != null && cursor.moveToFirst()) {
do {
if (cursor.getCount() > 0) {
list.add(Integer.parseInt(cursor.getString(1)));
} else {
list.add(0);
}
}
while (cursor.moveToNext());
}
Failure 4
if (cursor.moveToFirst()) {
do {
if (cursor != null && cursor.getCount() > 0) {
list.add(Integer.parseInt(cursor.getString(1)));
} else {
list.add(0);
}
}
while (cursor.moveToNext());
}
Failure 5
if (cursor.moveToFirst()) {
do {
list.add(Integer.parseInt(cursor.getString(1)));
}
while (cursor.moveToNext());
}
if (cursor.isBeforeFirst()){
do {
list.add(0);
}
while (cursor.moveToNext());
}