1

I have a RecyclerView list, and an items passes to it's adapter. Now when I click a row in this list, it will show the details of the pressed item.

Let's say I need to toggle the selected item as liked or disliked, as each item in the dataset will have isLiked property with either true or false value.

Now if I changed the isLiked value from false to true, I need to reflect the same change to the recyclerview in the parent - or the previous activity in the stack.

User will click 'back' from device and back to the list. But the changes made in the details activity is not reflecting on the previous list.

I was thinking of using Redux for state management, but not sure what is the ideal solution for this issue.

Any idea please?

Phantômaxx
  • 36,442
  • 21
  • 78
  • 108
Ammar Alrefai
  • 218
  • 1
  • 2
  • 7

3 Answers3

0

Use event bus for this Any problem paste the code take refrence http://greenrobot.org/eventbus/

First you need Pojo to get Notified suppose

public class NewsCategoryEvent extends RealmObject {
    @PrimaryKey
    String selectedCategory;
    boolean isSelected;

    public boolean isSelected() {
        return isSelected;
    }

    public void setSelected(boolean selected) {
        isSelected = selected;
    }

    public String getSelectedCategory() {
        return selectedCategory;
    }

    public void setSelectedCategory(String selectedCategory) {
        this.selectedCategory = selectedCategory;
    }
}

Then Notify EventBus after making changes in your activity in any method as:

      //newsCategoryEvent is object of your pojo 
      NewsCategoryEvent selectedNewsCategoryEvent = new NewsCategoryEvent ();
      newsCategoryEvent.setSelected(true);
    EventBus.getDefault().post(newsCategoryEvent);

When you back pressed you need to register and notify changes in your First Register your Eventbus in onStart();

  @Override
    protected void onStart() {
       super.onStart()
       EventBus.getDefault().register(this);
    }

Outside onCreate(): notifed recyclar view is updated

@Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(NewsCategory event) {
        newsListRecyclerViewAdapter.notifyDataSetChanged();
    }

and in onStop() unregister EventBus as

@Override
    public void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
}
  • Thanks for your answer. But in documentation they mention: ``` @Override public void onStop() { EventBus.getDefault().unregister(this); super.onStop(); } ``` If I unregistered it from parent activity it will not get notified. And if I disabled the unregister call, whenever I fire an action, let's say I fired it the third time, the subscriber will be called 3 times. Do you advise for solution? – Ammar Alrefai Jan 31 '18 at 12:30
  • Check out the code and also paste your code such that i can help you more – Sushil Chaudhary Feb 01 '18 at 06:10
  • Didn't you find the solution? – Sushil Chaudhary Feb 08 '18 at 03:42
0

As seen here, you need to call mAdapter.notifyDataSetChanged(); after you return from your "detail view", whatever it is.

ricardo silva
  • 282
  • 1
  • 15
0

If you want that your recycler view catch up changes you do on your details screen, be sure you have correctly defined DiffCallback on your recycler view so that isLike has been checked.

For example if you have you DiffCallback like:


class YourListAdapter : PagedListAdapter<YourItem, YourViewHolder>(DiffCallback) {



    companion object DiffCallback : DiffUtil.ItemCallback<YourItem>() {
        override fun areItemsTheSame(oldItem: YourItem, newItem: YourItem): Boolean {
            return oldItem.id == newItem.id
        }

        override fun areContentsTheSame(oldItem: YourItem, newItem: YourItem): Boolean {
            return oldItem.id == newItem.id && oldItem.isLike == newItem.isLike
        }
    }
}