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