1. 程式人生 > >Android cursor遍歷資料

Android cursor遍歷資料

資料儲存為一行多列時

Person person = new Person();
double amount = 0;
if (cursor != null && cursor.moveToFirst()) {
    int indexOfPerson = cursor.getColumnIndex(Calculate.COLUMN_PERSON);
    int indexOfAmount = cursor.getColumnIndex(Calculate.COLUMN_AMOUNT);

    String personStr = cursor.getString(indexOfPerson);
    person = JSONObject.parseObject(personStr, Person.class);

    amount = cursor.getDouble(indexOfAmount);

}

資料儲存為多行多列時

if (cursor != null && cursor.moveToFirst()) {
    Role[] roles = new Role[cursor.getCount()];
    int roleIndex = cursor.getColumnIndex(Role.COLUMN_ROLE);

    while (cursor.moveToNext()) {
        Role role = JSONObject.parseObject(cursor.getString(roleIndex), Role.class);
        roles[cursor.getPosition()] = role;
    }
    return
roles; }