Reputation: 2692
So I have a recycler view adapter set up in my MainActivity. That adapter is hooked up to an arraylist of a custom object I made called group.
public static RecyclerView.Adapter groupsListAdapter;
public ArrayList<Group> myGroups;
//inside onCreate of Main Activity
groupsListAdapter = new MyAdapter(myGroups);
In a different Activity, I am updating the contents of this arraylist, first by passing it through the intent.
Intent groupCreationIntent = new Intent(MainActivity.this, NewGroupActivity.class);
groupCreationIntent.putExtra("groupsList",myGroups);
startActivity(groupCreationIntent);
And then updating it in the other activity.
private ArrayList<Group> groups;
//in oncreate method of other activity
groups = (ArrayList<Group>) getIntent().getSerializableExtra("groupsList");
When going back to the previous activity (the second activity is used for entering some information and syncing it with my database) I run this code.
groups.add(myGroup);
MainActivity.groupsListAdapter.notifyDataSetChanged();
finish();
The problem is that the list adapter is not reflecting the changes, nothing is appearing in my list once i return to the main activity. What am I doing wrong?
Upvotes: 0
Views: 1088
Reputation: 1263
class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<Group> items;
public MyAdapter(List<Group> items) {
this.items = items;
}
// .. your implementation
// add and call this
public void add(Group item) {
this.items.add(item);
notifyItemInserted(items.size() - 1);
}
}
it's not related with your question, you should not use static field for Activity-to-Activity communication.
Upvotes: 1