我想从此函数返回dbManagement.ORIGINATING_ADDRESS的所有值,但它只给我最后一个值给blockedNumber。我知道它会给出最后一个值,但我怎样才能获得所有价值。请帮助
public String selectBlockedNumbers() {
Cursor cursor = dbManagement.selectBlockedNumbers();
String blockedNumber = null;
cursor.moveToFirst();
for(int i = 0; i < cursor.getCount(); i++) {
blockedNumber = cursor.getString(cursor.getColumnIndex(dbManagement.ORIGINATING_ADDRESS));
cursor.moveToNext();
}
Toast.makeText(this, blockedNumber, Toast.LENGTH_LONG).show();
return blockedNumber;
}
在
Cursor cursor = dbManagement.selectBlockedNumbers();
函数selectBlockedNumbers()包含以下查询:
cursor = db.rawQuery("SELECT " + ORIGINATING_ADDRESS + " FROM " + TABLE_BLOCK_LIST, null);
答案 0 :(得分:0)
当for循环最后一次运行时,blockedNumbers将包含检索到的最后一个数字。因此,不是返回String
,而是返回List<String>
。在这种情况下,您的代码应更改为
public List<String> selectBlockedNumbers() {
Cursor cursor = dbManagement.selectBlockedNumbers();
List<String> blockedNumbers = new ArrayList<String>();
cursor.moveToFirst();
for(int i = 0; i < cursor.getCount(); i++) {
String blockedNumber = cursor.getString(cursor.getColumnIndex(dbManagement.ORIGINATING_ADDRESS));
blockedNumbers.add(blockedNumber);
cursor.moveToNext();
}
Toast.makeText(this, blockedNumber, Toast.LENGTH_LONG).show();
return blockedNumbers;
}