有一個在數據庫中單行索引,需要收集分數。
23
45
84
56
然後在收集之後相加。
String[] projection2 = {BorrowMeTable.COLUMN_CREDIT_SCORE };
Cursor databaseCursor2 = getContentResolver().query(uri, projection2,
null, null, null);
int number = 0;
if (databaseCursor2 != null) {
while (databaseCursor2.isAfterLast() == false) {
databaseCursor2.moveToFirst();
number = number + databaseCursor2.getInt(databaseCursor2
.getColumnIndexOrThrow(BorrowMeTable.COLUMN_CREDIT_SCORE));
}
}
運行之後代碼就會崩潰,不知道為什麼?
在這裡結束循環:
while (databaseCursor2.isAfterLast() == false){
databaseCursor2.moveToFirst();
...
}
用下面的代替
while(cursor.moveToNext()) {
// do cursor stuff
....
}