0

I know that notifyItemInserted(position) is used, but in most of the examples I have seen it gets triggered with help of Click Listeners.

But in my case I want adapter to know the change and update its view when a button in another activity is pressed.

How can I achieve this?

Consider below example scenario: 1) App starts with Activity A

2) Activity A contains recyclerview

3) As Currently data is empty no items is shown in recyclerview

4) Somehow I got into Activity B

4) I updated the data and pressed Button

5) As new data is there, recyclerview is now having a single view with updated data

Gaurav Singh
  • 125
  • 3
  • 12
  • 3
    Consider SwipeRefreshLayout?? – Akshay May 05 '17 at 11:36
  • you can store the data in model while being in activity B, now coming back again in your adapter, you can use swiperefreshlayout and implement these methods, notifyDataSetChanged() or notifyItemChanged(int) or notifyItemRangeChanged(int, int) based on your need. refer this [link](http://stackoverflow.com/questions/33789345/whats-better-notifydatasetchanged-or-notifyitemchanged-in-loop) – Jay May 05 '17 at 11:44
  • No, that wouldn't work. I want the new view in RW to be appeared as I press button in Activity B.Something like Person fills the form-fields and submits and then finds that data in recyclerview in activity A – Gaurav Singh May 05 '17 at 11:51
  • Point is how can be adapter automatically informed about data update as soon as i press button in Activity B – Gaurav Singh May 05 '17 at 11:53
  • try using a static adapter in Activity A, and handle button click listener event in Activity B with mAdapter.notifyDataSetChanged() or implement logic as per your need: only specific item in a dataset or dataset as a whole. – Jay May 05 '17 at 11:58

2 Answers2

2
  1. Open activity B with startActivityForResult intent
  2. Come back From Activity B with your data
  3. In Activity A onActivityResult update your data and notify your adapter
MayurDev
  • 189
  • 2
  • 8
0

If it suits you, make a global instance of List of data model you want to update RecyclerView from.

List<DataModel> dataModelList = new ArrayList<>();

You can do this in a class extending Application class or somewhere else where you want.

Now in ActivityA

public class ActivityA extends AppCompatActivity {
    YourAdapter adapter;
    RecyclerView recyclerView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    recyclerView = (RecyclerView) findViewById(R.id.recyclerView);

    adapter = new YourAdapter(YourClassWhereYouPutDataModelList.dataModelList)
    recyclerView.setAdapter(adapter);


    @Override
    protected void onResume() {
        super.onResume();
        adapter.notifyDataSetChanged();
    }
}

Now in your ActivityB

public class ActivityA extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //your code to get value
                YourClassWhereYouPutDataModelList.dataModelList.add(YourValue);
                //Now it's up to you, either finish it using finish() or continue working
                //Whenever you go to ActivityA, RecyclerView will be updated
            }
        });
    }
}
Waqas Ahmed Ansari
  • 1,683
  • 15
  • 30