简体   繁体   English

从onActivityResult()调用时,notifyDataSetChanged()不能与我的RecyclerView.Adapter一起使用

[英]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. onActivityResult()用户返回到原来的活动之后叫我更新的数据RecyclerView并调用notifyDataSetChanged()onBindViewHolder()没有被调用, RecyclerView不更新。

If I run the same code to update the Recylerview from an onClick() trigger the RecyclerView does update properly. 如果我运行相同的代码来从onClick()触发器更新Recylerview,则RecyclerView 正确更新。 It's only when the updating code is called from onActivityResult() that the RecylerView does not update. 只有当从onActivityResult()调用更新代码时, RecylerView才会更新。

I tried updating the RecylerView by running the update method using the runOnUiThread() method but that didn't fix the issue. 我试图更新RecylerView运行使用的更新方法runOnUiThread()方法,但没有解决问题。 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. 我还尝试了RecyclerView.Adapter所有相关通知方法(即notifyDataSetChanged()等),但为简单起见,我将仅引用notifyDataSetChanged

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. 看当refreshData()方法在通过onClick()触发器调用时正确更新RecyclerView ,似乎该方法配置正确。 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. 我尝试在onActivityResult添加延迟,这会让RecylervView在运行refreshData()方法之前加载任何数据,但这并没有解决问题。

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. 我已经查看了其他SO问题,但我找不到适用于此问题的解决方案。

Thanks in advance 提前致谢

Ensure that you have called the: 确保您已拨打:

finish();

after the setResult : setResult

setResult(Adapter.REQUEST_CODE, usefulIntent);

In order to trigger the onActivityResult . 为了触发onActivityResult

Also if the: 如果:

notifyDataSetChanged();

isn't working consider to reset the 没有工作考虑重置

setAdapter();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 RecyclerView.Adapter 中使用 onActivityResult - How to use onActivityResult in RecyclerView.Adapter 如何使用 AsyncTask、RecyclerView 和 RecyclerView.Adapter 将手机中的图像获取到 RecyclerView? - How can I use AsyncTask, RecyclerView, and a RecyclerView.Adapter to get images from my phone into my RecyclerView? Recyclerview适配器的notifyDataSetChanged不更新RecyclerView - Recyclerview Adapter's notifyDataSetChanged not updating RecyclerView 当我从sqlite读取时,notifyDataSetChanged不起作用 - notifyDataSetChanged doesn't work when i am reading from sqlite 什么是 RecyclerView.Adapter<MyAdapter.MyViewHolder> 以及它与 Android 中的 RecyclerView.Adapter 有何不同? - What is RecyclerView.Adapter<MyAdapter.MyViewHolder> and how it is different from RecyclerView.Adapter in Android? notifyDataSetChanged在适配器上不起作用 - notifyDataSetChanged didn't work on adapter onActivityResult 未从适配器调用 - onActivityResult not getting called from adapter notifyDataSetChanged()不起作用 - notifyDataSetChanged() doesn't work 为什么我的RecyclerView没有显示任何内容? /如果在调用notifyDataSetChanged()之后再次设置了回收站适配器,会发生什么情况? - Why is my RecyclerView not showing anything? / What happend if Recycler Adapter is set again after notifyDataSetChanged() called? 无法在我的类中使用 onBindViewHolder 实现 extends RecyclerView.Adapter - cannot implements with the onBindViewHolder in my class extends RecyclerView.Adapter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM