繁体   English   中英

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

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

onActivityResult()用户返回到原来的活动之后叫我更新的数据RecyclerView并调用notifyDataSetChanged()onBindViewHolder()没有被调用, RecyclerView不更新。

如果我运行相同的代码来从onClick()触发器更新Recylerview,则RecyclerView 正确更新。 只有当从onActivityResult()调用更新代码时, RecylerView才会更新。

我试图更新RecylerView运行使用的更新方法runOnUiThread()方法,但没有解决问题。 我还尝试了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

setResult(Adapter.REQUEST_CODE, usefulIntent);

为了触发onActivityResult

如果:

notifyDataSetChanged();

没有工作考虑重置

setAdapter();

暂无
暂无

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

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