[英]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.