java—为什么不在bindview中使用movetofirst()?

s2j5cfk0  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(289)

实施时 onLoadFinished() ,它需要 moveToFirst() 很好地工作,但是为什么在实现时不需要这样做呢 bindView() 为了 CursorAdapter ? 什么时候用?
加载完成:

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

k4emjkb11#

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描述返回false: Move the cursor to the first row. This method will return false if the cursor is empty. onloadfinished不是cursoradapter的成员,因此不适合这样做。
你好,迈克

相关问题