简体   繁体   中英

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

When implementing onLoadFinished() , it requires moveToFirst() to work well, but why isn't this required when implementing bindView() for CursorAdapter ? And when to use it?

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));

    }
}

bindView:

@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);
}

The API explicitly states in CursorAdapter.bindView: @param cursor The cursor from which to get the data. The cursor is already moved to the correct position. @param cursor The cursor from which to get the data. The cursor is already moved to the correct position. So the moveToFirst is already done for you. The action is needed to advance in the records returned from the query on the database. If no records are found, moveToFirst will return false as per API description: Move the cursor to the first row. This method will return false if the cursor is empty. Move the cursor to the first row. This method will return false if the cursor is empty.

The onLoadFinished isn't a member of the CursorAdapter and thus not tailored to do so.

Regards, Mike

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM