java - 当使用 CursorAdapter 的回调时,调用 initLoader() 仅有效一次

标签 java android callback android-cursoradapter android-cursorloader

在我的 Activity 中,我有一个代表医生的 ListView 。每行都有医生的姓名和一个复选框。我实现了一个回调接口(interface),这样当选择一个医生时,所有其他医生都会从 ListView 中删除,只保留选定的医生。

它似乎有效,因为一旦我选择了一位医生,所有其他医生都会被删除。当我取消选中医生时,每个人都会被添加回来。但是,如果我现在选择一位不同医生,原来的医生会保留,所有其他医生都会被删除。

为了更好地解释这个问题,假设当我开始 Activity 时, ListView 中有两位医生:Joel 和 Sam。我想我想选择 Joel,所以我选择了,而 Sam 则从列表中删除。然后,我意识到我错了,所以我取消选择 Joel,现在我在列表中看到 Joel 和 Sam。最后,我选择 Sam。然而,Sam 已从列表中删除,只剩下 Joel。

以下是适配器类的一些代码 fragment :

@Override
public void bindView(View view, Context context, Cursor cursor) {
    ViewHolder viewHolder = (ViewHolder) view.getTag();

    final long id = cursor.getLong(cursor.getColumnIndex(DoctorEntry._ID));
    String firstName = cursor.getString(cursor.getColumnIndex(DoctorEntry.COLUMN_FIRSTNAME));

    viewHolder.nameView.setText(firstName);

    viewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(mCallbacks != null){
                if(isChecked){
                    mCallbacks.onDoctorChecked(id);
                } else{
                    mCallbacks.onDoctorUnchecked();
                }
            }
        }
    });
}

public void onRegisterCallbacks(DoctorAdapterCallbacks activity){
    mCallbacks = activity;
}

public static interface DoctorAdapterCallbacks{
    void onDoctorChecked(long id);

    void onDoctorUnchecked();
}

在我的 Activity 中,我有以下实现:

@Override
public void onDoctorChecked(long id) {
    Bundle args = new Bundle();
    args.putLong(SELECTED_DOCTOR_ID, id);
    getSupportLoaderManager().initLoader(SELECTED_DOCTOR_LOADER, args, this);
}

@Override
public void onDoctorUnchecked() {
    getSupportLoaderManager().initLoader(DOCTOR_LOADER, null, this);
}

DOCTOR_LOADER 是一个 CursorLoader,代表表中的所有医生。 SELECTED_DOCTOR_ID 是仅针对单个医生的 CursorLoader。

如果我不得不猜测,我的问题在于 bindView 方法,因为我将 id 变量声明为最终变量。我这样做的原因是否则我会收到编译器错误:

error: local variable id is accessed from within inner class; needs to be declared final

声明变量 final 是否会造成麻烦?有人发现问题吗?

编辑

我已将日志语句添加到适配器中的 onCheckedChangedListener 和 Activity 的 onDoctorSelected 中。使用上面的相同示例,我看到以下输出:

> onCheckedChanged : Selecting doctor id: 1 // Joel
> onDoctorSelected : Selecting doctor id: 1 // Joel
> onCheckedChanged : Selecting doctor id: 2 // Sam
> onDoctorSelected : Selecting doctor id: 2 // Sam

所以,它似乎确实看到我选择了 Sam,id 为 2,并将 id 2 传递到 initLoader() 方法中,但 ListView 中只显示 Joel(医生 id 1),因为我首先选择了他。

编辑2

根据请求,以下是 CursorLoader 方法的 fragment :

@Override
public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
    switch(i){
        case DOCTOR_LOADER:
            return new CursorLoader(
                    this,
                    DoctorEntry.CONTENT_URI,
                    DOCTOR_COLUMNS,
                    null,
                    null,
                    null
            );
        case SELECTED_DOCTOR_LOADER:
            long _id = bundle.getLong(SELECTED_DOCTOR_ID);
            return new CursorLoader(
                    this,
                    DoctorEntry.buildDoctorUri(_id),
                    DOCTOR_COLUMNS,
                    DoctorEntry._ID + " = '" + _id + "'",
                    null,
                    null
            );
        default:
            throw new UnsupportedOperationException("Unknown loader id: " + i);
    }
}

@Override
public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) {
    switch(cursorLoader.getId()){
        case DOCTOR_LOADER:
        case SELECTED_DOCTOR_LOADER:
            mDoctorAdapter.swapCursor(cursor);
            break;
        default:
            throw new UnsupportedOperationException("Unknown loader id: " + cursorLoader.getId());
    }
}

编辑3

我在 onCreateLoader 中添加了另一个日志语句,以查看正在使用哪个 ID 创建加载程序。然后,我看到了这个输出:

> onCheckedChanged : Selecting doctor id: 2
> onDoctorSelected : Selecting doctor id: 2
> onCreateLoader : Using id: 2
> // Unchecked, and now check doctor one
> onCheckedChanged : Selecting doctor id: 1
> onDoctorSelected : Selecting doctor id: 1

这不是一个错字。看起来 onCreateLoader 在第二次选择医生时没有被调用。我尝试在 onDoctorChecked 内调用 destroyLoader(),但这似乎没有什么区别。

最佳答案

根据initLoader() documentation :

Ensures a loader is initialized and active. If the loader doesn't already exist, one is created and (if the activity/fragment is currently started) starts the loader. Otherwise the last created loader is re-used.

In either case, the given callback is associated with the loader, and will be called as the loader state changes. If at the point of call the caller is in its started state, and the requested loader already exists and has generated its data, then callback onLoadFinished(Loader, D) will be called immediately (inside of this function), so you must be prepared for this to happen.

initLoader() 仅初始化某个加载器 ID 一次,然后重用此后的数据。如果您想扔掉并重新创建一个新的加载器(即使用新的 CursorLoader),请使用 restartLoader() (注意:restartLoader() 将像第一次 initLoader() 一样初始化加载器,因此第一次运行时不需要任何特殊逻辑) .

关于java - 当使用 CursorAdapter 的回调时,调用 initLoader() 仅有效一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27894572/

相关文章:

java - 使用 ArgumentCaptor<List> 和 hamcrest.hasSize

java - 如何打印出 JTable 的特定行/列?

java - 关闭java应用程序时表单不会消失

android - ViewModel Observer 第一个参数是 require Lifecycleowner

c++ - 来自对象的 freeglut 中的回调函数

javascript - 将索引从 for 循环传递到 ajax 回调函数 (JavaScript)

Java 我 : easiest way to format strings?

javascript - 如何制作目标宽度和高度大于 640px 的高质量 PhoneGap 相机图像?

java - 捕获或从图像库中选取的图像将照片旋转 90 度

javascript - 无法将 phantom.exit() 放入 page.evaluate() 中的 phantomjs