I have a pretty unique situation, where I have a button in a recyclerview, which upon click (initial state "register"), passes an intent to a broadcast receiver in fragment or activity where it throws an alert dialog , with 2 options, yes or no. If no is selected, nothing happens and the dialog dismisses, but if yes is clicked, it processes a function I defined in my presenter class (related to data) and is supposed to update my button ui state to "cancel". and same goes for the other way around where upon clicking cancel it will bring back alert, and clicking on yes will switch the ui text to register.
Now I have implemented the code as follows.(please note, even notifydatasetchanged doesnt work for me). Any idea what I am doing wrong and how I can achieve this? code in my public void onBind(int position) function in adapter:
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);
});
}
}
}
Here's the corresponding code in broadcast receiver in my 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();
}
}
};
Please note: mEventListAdapter is my adapter i am using the button UI code in and mEventRecyclerView is the recycler view i am using in the fragment.
Any idea what I am missing or doing wrong? Thanks!
RSVPClick method :
@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);
}
}));
}
from How do I update my RecyclerView button state on click after alert dialog confirmation in activity/fragment?
No comments:
Post a Comment