Java notifyDataSetChanged()不';当我的RecyclerView.Adapter';从onActivityResult()调用的

Java notifyDataSetChanged()不';当我的RecyclerView.Adapter';从onActivityResult()调用的,java,android,android-recyclerview,onactivityresult,notifydatasetchanged,Java,Android,Android Recyclerview,Onactivityresult,Notifydatasetchanged,当用户返回到原始活动后调用onActivityResult()时,我更新RecyclerView的数据,并调用notifyDataSetChanged(),但未调用onBindViewHolder(),且RecyclerView不更新 如果我从onClick()运行相同的代码来更新Recylerview,则会触发RecyclerView是否正确更新。只有在从ActivityResult()调用更新代码时,RecyleView才不会更新 我尝试使用runOnUiThread()方法运行update

当用户返回到原始活动后调用
onActivityResult()
时,我更新
RecyclerView
的数据,并调用
notifyDataSetChanged()
,但未调用
onBindViewHolder()
,且
RecyclerView
不更新

如果我从
onClick()
运行相同的代码来更新Recylerview,则会触发
RecyclerView
是否正确更新。只有在从ActivityResult()调用更新代码时,
RecyleView
才不会更新

我尝试使用
runOnUiThread()
方法运行update方法来更新
RecylerView
,但没有解决问题。我还尝试了
RecyclerView.Adapter
的所有相关通知方法(即
notifyDataSetChanged()
等),但为了简单起见,我将只参考
notifyDataSetChanged

以下是问题的基本再现:

   //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);
     }
 }
由于
refreshData()
方法在通过
onClick()
触发器调用时确实正确地更新了
RecyclerView
,因此该方法似乎配置正确。我尝试向
onActivityResult
添加延迟,这将使
RecylervView
在运行
refreshData()
方法之前有时间加载任何数据,但这并没有解决问题

有没有人能看到我的代码中有什么问题,或者告诉我如何解决这个问题

我已经研究了其他的SO问题,但是我找不到一个适用于这个问题的解决方案


提前感谢

请确保您已致电:

finish();
设置结果之后

setResult(Adapter.REQUEST_CODE, usefulIntent);
为了触发激活结果

此外,如果:

notifyDataSetChanged();

不工作,考虑重置

setAdapter();

谢谢你的回复。我尝试在调用setResult()之后调用finish(),但是在调用notifyDataSetChanged()之后仍然没有调用onBindViewHolder(),并且RecyclerView仍然没有更新。关于onBindViewHolder()为什么不会被触发,你还有其他想法吗?@MicahSimmons你能从refreshData()调用recyclerView.setAdapter(arrayListData)吗?重新初始化适配器使recyclerView得到了正确更新,我现在明白了为什么适配器之前没有更新,所以问题得到了解决。谢谢你的帮助help@MicahSimmons您需要一次又一次地调用
recyclerView.setAdapter
,这是一种workaround@pskink你说得对,我不需要调用
recyclerView.setAdapter
来更新recyclerView,但是调用setAdapter()确实刷新了recyclerView,我发现了导致notifyDataSetChanged()无法更新RecyclerView的问题,并修复了该问题。我运行了一个简单的测试,它可以正常工作,请尝试清除您的
onActivityResult
代码感谢您的回复。我将查看onActivityResult()的设置,我可以从日志语句中看到,onActivityResult()是用预期的请求代码调用的,refreshData()是由onActivityResult()触发的,我可以看到数据已从arrayList中删除,但在调用NotifyDataSanged()后,不会调用onBindViewHolder