【问题标题】:Why don't we use moveToFirst() in bindView?为什么我们不在 bindView 中使用 moveToFirst()?
【发布时间】:2020-11-13 23:07:20
【问题描述】:

在实现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));

    }
}

绑定视图:

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

【问题讨论】:

    标签: java android sqlite cursor android-sqlite


    【解决方案1】:

    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 的成员,因此不适合这样做。

    问候,迈克

    【讨论】:

    • 非常感谢您的回答
    猜你喜欢
    • 2011-03-10
    • 2011-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-08
    • 2016-07-23
    • 2018-09-17
    相关资源
    最近更新 更多