为什么我们不在 bindView 中使用 moveToFirst()?

Why don't we use moveToFirst() in bindView?

实施onLoadFinished()时,需要moveToFirst()才能正常工作,但为什么实施bindView()时不需要CursorAdapter呢?什么时候使用它?

onLoadFinished:

@Override
public void onLoadFinished(@NonNull Loader loader, Cursor data) {
    if (data.moveToFirst()) {
        int nameColumnIndex = data.getColumnIndexOrThrow(PetEntry.COLUMN_PET_NAME);
        int breedColumnIndex = data.getColumnIndexOrThrow(PetEntry.COLUMN_PET_BREED);

        mNameEditText.setText(data.getString(nameColumnIndex));
        mBreedEditText.setText(data.getString(breedColumnIndex));

    }
}

绑定视图:

@Override
public void bindView(View view, Context context, Cursor cursor) {
    TextView name = view.findViewById(R.id.name);
    TextView summary = view.findViewById(R.id.summary);

    String nameString = cursor.getString(cursor.getColumnIndexOrThrow(PetEntry.COLUMN_PET_NAME));
    String summaryString = cursor.getString(cursor.getColumnIndexOrThrow(PetEntry.COLUMN_PET_BREED));

    name.setText(nameString);
    summary.setText(summaryString);
}

API 在 CursorAdapter.bindView 中明确指出: @param cursor The cursor from which to get the data. The cursor is already moved to the correct position. 所以 moveToFirst 已经为你完成了。需要该操作才能在数据库查询中 returned 的记录中前进。如果没有找到记录,moveToFirst 将 return false 根据 API 描述:Move the cursor to the first row. This method will return false if the cursor is empty.

onLoadFinished 不是 CursorAdapter 的成员,因此不适合这样做。

此致,迈克