Mar*_*Rey 5 sqlite android arraylist android-sqlite
我有一个使用游标运行SQlite查询的应用程序.
public Cursor totaltrips(){
Cursor cursor = database.rawQuery("SELECT * AS TTotal FROM " + DatabaseHelper.TABLE_NAME, null);
return cursor;
}
Run Code Online (Sandbox Code Playgroud)
结果存储到最多5个值的Arraylist.如果数据库中没有记录,则应用程序崩溃.如果我有一个或多个数据库条目,它工作正常.有没有人知道如何在没有数据库条目时阻止它崩溃?
// get column value
if (Distance.moveToNext())
result = String.valueOf(Distance.getDouble(Distance.getColumnIndex("myTotal")));
tnmView.setText(result);
List<String> distancearray = new ArrayList<String>();
Cursor cursor = dbManager.totaldistance();
do{
distancearray.add(cursor.getString(1));
}while ((cursor.moveToNext()));
ttrips = cursor.getCount();
Log.i("Graph", "TTRIPS = " + ttrips);
// Be sure here to have at least the 5 desired elements into the list
while(distancearray.size() < 5){
distancearray.add("0");
}
Run Code Online (Sandbox Code Playgroud)
该应用程序因错误而崩溃
android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
Run Code Online (Sandbox Code Playgroud)
在线上
do{
distancearray.add(cursor.getString(1));
}while ((cursor.moveToNext()));
Run Code Online (Sandbox Code Playgroud)
检查光标是否确实有结果,尝试这样的例子:
int numResults = cursor.getCount();
if (numResults > 0) {
do {
distancearray.add(cursor.getString(1));
} while ((cursor.moveToNext()));
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
451 次 |
最近记录: |