在实现onLoadFinished()
时,它需要moveToFirst()
很好地工作,但是为什么在为CursorAdapter
实现bindView()
时不需要这样做呢?什么时候使用它?
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);
}
发布于 2020-12-20 23:19:20
API在CursorAdapter.bindView:@param cursor The cursor from which to get the data. The cursor is already moved to the correct position.
中显式地声明,因此moveToFirst
已经为您完成了。在数据库上的查询返回的记录中,需要执行此操作。如果没有找到记录,moveToFirst将按照API描述:Move the cursor to the first row. This method will return false if the cursor is empty.
返回false
onLoadFinished并不是CursorAdapter的成员,因此不适合这样做。
你好,迈克
https://stackoverflow.com/questions/64829157
复制