简体   繁体   中英

notifyDataSetChanged() doesn't work with my RecyclerView.Adapter when it's called from onActivityResult()

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 (ie 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

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM