java - 在 Activity/fragment 中确认警报对话框后,如何在单击时更新我的​​ RecyclerView 按钮状态?

标签 java android broadcastreceiver android-recyclerview

我有一个非常独特的情况,我在 recyclerview 中有一个按钮,点击后(初始状态“注册”),将一个 Intent 传递给 fragment 或 Activity 中的广播接收器,它会抛出一个警报对话框,带有 2选项,是或否。如果选择否,则不会发生任何事情并且对话框消失,但如果单击是,它将处理我在我的演示者类中定义的函数(与数据相关)并且应该将我的按钮 ui 状态更新为“取消”。反之亦然,点击取消会返回警报,点击是会将 ui 文本切换为注册。

现在我已经实现了如下代码。(请注意,即使 notifydatasetchanged 对我也不起作用)。知道我做错了什么以及如何实现这一目标吗? 适配器中我的 public void onBind(int position) 函数中的代码:

if (repo.getIsRsvpAvailable().equals("true")) {
    rsvpButton.setVisibility(View.VISIBLE);
    for (int i = 0; i < mAllRSVPEventsList.size(); i++) {
        if (mAllRSVPEventsList.get(i).getEvent().getEventId().equals(repo.getEventId()) && mAllRSVPEventsList.get(i).getIsAttending()) {
            rsvpButton.setText("CANCEL");
            rsvpButton.setOnClickListener(v -> {
                Intent in = new Intent("main_rsvp_button_clicked");
                in.putExtra("main_rsvp_event_id", repo.getEventId());
                in.putExtra("main_rsvp_is_attending", "false");
                in.putExtra("main_rsvp_item_position", position);
                rsvpButton.getContext().sendBroadcast(in);
            });
            break;
        } else {
            rsvpButton.setText("RSVP");
            rsvpButton.setOnClickListener(v -> {
                Intent in = new Intent("main_rsvp_button_clicked");
                in.putExtra("main_rsvp_event_id", repo.getEventId());
                in.putExtra("main_rsvp_is_attending", "true");
                in.putExtra("main_rsvp_item_position", position);

                rsvpButton.getContext().sendBroadcast(in);
            });
        }
    }

}

这是我 Activity 中广播接收器中的相应代码:

private BroadcastReceiver mEventIdReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        try {
            String eventId = intent.getStringExtra(EVENTID_MAIN_EXTRA_TITLE);
            String isAttending = intent.getStringExtra(EVENTID_MAIN_IS_ATTENDING);
            int itemPosition = intent.getIntExtra(EVENTID_MAIN_RSVP_ITEM_POSITION, 0);


            if (isAttending.equals("true")) {
                showDialog(R.string.rsvp_title, R.string.confirm_rsvp_body, R.string.yes,
                        (dialog, which) -> {
                            mPresenter.onRSVPClick(eventId, isAttending);

                            mEventListAdapter.notifyDataSetChanged();
                            mEventRecyclerView.removeAllViews();
                            mEventRecyclerView.scrollToPosition(itemPosition);

                        }, R.string.no, null, null);
            } else {
                showDialog(R.string.rsvp_title, R.string.confirm_cancel_body, R.string.yes,
                        (dialog, which) -> {
                            mPresenter.onRSVPClick(eventId, isAttending);

                            mEventListAdapter.notifyDataSetChanged();
                            mEventRecyclerView.removeAllViews();
                            mEventRecyclerView.scrollToPosition(itemPosition);

                        }, R.string.no, null, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
};

请注意:mEventListAdapter 是我在其中使用按钮 UI 代码的适配器,mEventRecyclerView 是我在 fragment 中使用的回收器 View 。

知道我遗漏了什么或做错了什么吗? 谢谢!

RSVPClick 方法:

@Override
public void onRSVPClick(String eventId, String isAttending) {
    getMvpView().showLoading();
    getCompositeDisposable().add(getDataManager()
            .doRSVPEventApiCall(
                    eventId,
                    getDataManager().getFirstName(),
                    getDataManager().getLastName(),
                    getDataManager().getCurrentUserEmail(),
                    isAttending
            )
            .subscribeOn(getSchedulerProvider().io())
            .observeOn(getSchedulerProvider().ui())
            .subscribe(response -> {
                if (!isViewAttached()) {
                    return;
                }
                getMvpView().hideLoading();
            }, throwable -> {
                if (!isViewAttached()) {
                    return;
                }
                getMvpView().hideLoading();
                if (throwable instanceof ANError) {
                    ANError anError = (ANError) throwable;
                    handleApiError(anError);
                }
            }));
}

最佳答案

在调用 notifyDatasetChanged() 之前,您是否根据用户在对话框中选择的操作更新了 repo 对象? 看起来 doRSVPEventApiCall() 方法不会更新适配器可用的项目列表,因此 notifyDatasetChanged() 无效。

关于java - 在 Activity/fragment 中确认警报对话框后,如何在单击时更新我的​​ RecyclerView 按钮状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57332222/

相关文章:

android - 将当前时间传递给 OpenGL ES 2.0 着色器以进行纹理动画 : animation stops after certain time

android - 应用程序卸载时无法获取接收器

android - BroadcastReceiver 的 Manifest 和 Programmatic 注册的主要区别

android - 未调用 BroadcastReceiver (Kotlin)

java - 错误消息 ORA-00933 : SQL command not properly ended

Android 从源代码编译 webkit 并在应用程序中使用它

java - 在java 8中按多个字段名分组

android - 在 Android 的 doinbackground() 中执行 UI 任务

java - Android Studio:运行我的应用程序时出现:“Could not create the Java Virtual Machine”错误

java - 在 2D JButton 阵列上添加图像