3

When onActivityResult() is called after a user returns to the original activity I update the data for the RecyclerView and call notifyDataSetChanged() but onBindViewHolder() is not being called and the RecyclerView does not update.

If I run the same code to update the Recylerview from an onClick() trigger the RecyclerView does update properly. It's only when the updating code is called from onActivityResult() that the RecylerView does not update.

I tried updating the RecylerView by running the update method using the runOnUiThread() method but that didn't fix the issue. I have also tried all the relevant notify methods (i.e. notifyDataSetChanged() etc. ) of the RecyclerView.Adapter but I will just refer to the notifyDataSetChanged for simplicity.

Here is a basic reproduction of the problem:

   //This code is in the Adapter, it removes an item from the arrayList and updates the RecylerView.     
     protected void refreshData(int position){
        arrayListData.remove(position);
        notifyDataSetChanged ();
    }


    //This code is in the ViewHolder. When refreshData() is called via the onClick() here the **RecylerView does successfully update** 
     @Override
            public void onClick(View v) {        
             if (shouldRefreshData == true) {     
               refreshData(getAdapterPosition()); 
          } else {
                Intent secondActivity = new Intent(context, SecondActivity.class);
((Activity)context).startActivityForResult(secondActivity, Adapter.REQUEST_CODE); 
         }
    }

//I set the result code is in the Second Activity like this
 setResult(Adapter.REQUEST_CODE, usefulIntent);


//This code is in the original activity, it successfully runs, and the refreshData() method is called and I can see the data has been removed via log statements in the refreshData() method but the onBindViewHolder() method is never called
@Override
    protected void onActivityResult(int requestCode, int resultCode, final Intent data) {
         if (requestCode == Adapter.REQUEST_CODE) {
    ....
    adapter.refreshData(positionRetrievedFromTheDataIntent);
     }
 }

Seeing as the refreshData() method does properly update the RecyclerView when it's called via an onClick() trigger it seems that that method is configured properly. I tried adding delay to the onActivityResult which would give the RecylervView time load any data before running the refreshData() method but that didn't fix the issue.

Can anyone see any problems in my code or tell me how to fix this problem?

I have looked over other SO questions but I couldn't find an applicable solution to this problem.

Thanks in advance

Micah Simmons
  • 2,078
  • 2
  • 20
  • 38
  • i run a simple test and it just works, try to debuf your `onActivityResult` code – pskink Jun 19 '16 at 16:58
  • Thanks for your response. I'll look over my setup of onActivityResult() – Micah Simmons Jun 19 '16 at 17:00
  • I can see from Log statements that onActivityResult() is called with the expected requestCode, and that refreshData() is triggered by onActivityResult() , and I can see that the data is removed from the arrayList, but onBindViewHolder isn't being called after notifyDataSetChanged() is called – Micah Simmons Jun 19 '16 at 17:10

1 Answers1

1

Ensure that you have called the:

finish();

after the setResult:

setResult(Adapter.REQUEST_CODE, usefulIntent);

In order to trigger the onActivityResult.

Also if the:

notifyDataSetChanged();

isn't working consider to reset the

setAdapter();
Lucas Moretto
  • 404
  • 1
  • 6
  • 18
  • Thanks for your response. I tried calling finish() after calling setResult() but onBindViewHolder() still isn't called after notifyDataSetChanged() is called and the RecyclerView still doesn't update. Have you got any other ideas about why onBindViewHolder() wouldn't be getting triggered? – Micah Simmons Jun 19 '16 at 16:54
  • 1
    @MicahSimmons Can you call the recyclerView.setAdapter(arrayListData) from the refreshData()? – Alexios Karapetsas Jun 19 '16 at 17:12
  • 1
    Re-initialising the Adapter got the RecyclerView to update properly, and I now understand why the adapter wasn't updating before, so the problem's been solved. Thanks for your help – Micah Simmons Jun 19 '16 at 17:42
  • @MicahSimmons you font need to call `recyclerView.setAdapter` again and again, its kind of workaround – pskink Jun 19 '16 at 18:10
  • @pskink you're right, I didn't need to call `recyclerView.setAdapter` to update the RecyclerView, but, calling setAdapter() did refresh the RecyclerView, and I found the problem that was causing notifyDataSetChanged() to not update the RecyclerView and fixed it – Micah Simmons Jun 19 '16 at 18:18
  • @MicahSimmons so what was the culprit? – pskink Jun 19 '16 at 18:20
  • I had created 2 separate instances of Adapter, and I was calling the refreshData() method on the Adapter that hadn't been set to the RecyclerView. I might have changed my code at some point and I was passing `new Adapter()` into setAdapter() instead of passing in the member variable `adapter`. Presumably the reason why the onClick() trigger was able to update the RecyclerView was because it is a method that was being called from within the Adapter class which ensured that it was always updating the Adapter that had been assigned to the RecyclerView – Micah Simmons Jun 19 '16 at 18:28
  • @MicahSimmons I have updated the answer. If you think that it has helped you to solve the problem, feel free to accept it as an answer. – Alexios Karapetsas Jun 19 '16 at 21:12
  • I won't accept it as the answer, it's more of a workaround than the solution to the actual problem, but I'll up vote your answer, your answer could potentially solve the issue – Micah Simmons Jun 19 '16 at 22:29