【问题标题】:SimpleCursorAdapter, Swapping cursor in API level below 11SimpleCursorAdapter,在 API 级别低于 11 时交换光标
【发布时间】:2022-01-04 22:47:34
【问题描述】:

尝试实现 LoaderManager + CursorLoader。

在 onFinish 方法中适配器应该交换它的光标

  public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    // Swap the new cursor in.  (The framework will take care of closing the
    // old cursor once we return.)
    mAdapter.swapCursor(data);
  }

但是 swapCursor 从 API 级别 11 开始可用。

那么我应该如何在 android API 10 中交换光标?

【问题讨论】:

    标签: android android-cursor


    【解决方案1】:
    【解决方案2】:

    如果您按照 Android Studio 的建议进行换行,并且 swapCursor 解释旧光标未关闭,使用 android.widget.CursorAdapter,您会得到:

    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            mAdapter.swapCursor(data);
        } else {
            Cursor oldCursor = mAdapter.getCursor();
            mAdapter.changeCursor(data);
            oldCursor.close();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多