Harish Chauhan
Harish Chauhan

Reputation: 113

Adapter values is not updated from another activity

I am stuck with a problem where I need to update the value of a adapter variable in another activity and coming back to first activity should also get that updated value.

my current flow is like. I am staring the BarDetailsActivity and passing the modal with intent from inside the adapter class like:

  Intent barDetailIntent = new Intent(getApplicationContext(), BarDetailActivity.class);
  barDetailIntent.putExtra("isfav", barsList.get(position));
  barDetailIntent.putParcelableArrayListExtra("barlist",barsList);
  mContext.startActivity(barDetailIntent);

Then on another activity I am getting that model from intent and changing its variable values as:

 gbar = in.getParcelableExtra("isfav");
 blist= in.getParcelableArrayListExtra("barlist");
 if (gbar.getmFavourite()) {    
    gbar.setmFavourite(false);
    } else {
   gbar.setmFavourite(true);
    }

Now on going back to my main Activity Value for "gbar.setmFavourite" is not updated on onresume of the MainActivity.

@Override
protected void onResume() {
    super.onResume();
    if(mAdapter != null){
        mAdapter.notifyDataSetChanged(); // here the adapter value is not updated.
    }
}

Please help me on this.

Upvotes: 0

Views: 71

Answers (1)

nnn
nnn

Reputation: 1000

Use Event Bus to handle the problem.

  1. Register your First Activity to listen the Event and override onEvent method.
  2. Fire the stickyIntent with updated dataset from SecondActivity.
  3. In the onEvent method of the FirstActivity call notifyDataSetChanged with the updated dataset.

Upvotes: 1

Related Questions