繁体   English   中英

在包含共享相同数据集的适配器的活动之间进行转换的问题

[英]Issue with transitioning between activities containing adapters sharing the same data set

给定2个活动,A从结果开始B.

这两个活动都有结构(A:RecyclerView,B:ViewPager)和连接到同一数据集的适配器,存储在Application对象中。

B完成并将结果发布回A.

A在onActivityResult()上获取结果,并在函数体中更改数据集(删除一个元素)。

问题是稍后(在调试器退出我的代码之后)我在B中崩溃说:

java.lang.IllegalStateException: The application's PagerAdapter changed the adapter's contents without calling PagerAdapter#notifyDataSetChanged! Expected adapter item count: 10, found: 9

堆栈跟踪以:

android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2271)
android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1358)
android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1607)
android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1246)
android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6301)
android.view.Choreographer$CallbackRecord.run(Choreographer.java:871)
android.view.Choreographer.doCallbacks(Choreographer.java:683)
android.view.Choreographer.doFrame(Choreographer.java:619)
android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:857)
android.os.Handler.handleCallback(Handler.java:751)
android.os.Handler.dispatchMessage(Handler.java:95)
android.os.Looper.loop(Looper.java:154)`

不确定这里发生了什么......在调用完成后调用A.onActivityResult()之前,活动B不应该永远消失吗?

这是因为退出动画需要B的最终抽奖吗?

编辑:B.onStop()和B.onDestroy()在A.onActivityResult()之后调用。 为什么这是正常行为?

尝试在调用适配器中的getCount()之前调用notifyDataSetChanged()。

像这样的东西:

@Override
public int getCount() {

  notifyDataSetChanged();

  return list.size();

}

暂无
暂无

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

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