从 Parcelable ArrayList 重新填充列表

发布于 2024-11-25 04:26:31 字数 935 浏览 2 评论 0原文

我有一个使用 ArrayAdapter 的 ListView。 ArrayAdapter 中的项目是我创建的自定义对象。

我已经从 Parcelable 扩展了这个自定义对象。当用户旋转屏幕并保存instanceState时,会将ArrayList写入outState。重新创建 ListActivity 时,instanceState 会读回 ArrayList。在 onResume() 中,我将 ArrayList 分配给 ArrayAdapter 的 items 变量,然后使用 ArrayAdapter 在 ListView 上调用 setAdapter。

不幸的是,ListView 中没有显示任何内容。

我已经调试完毕,在调用 setAdapter 之前一切似乎都工作得很好。 ArrayList 从instanceState 读回,所有数据都在那里并完美保存。

有什么想法吗?

更新:

我已经解决了这个问题。我会尝试在这里写一个解释,直到 stackoverflow 让我创建一个答案。

在 onCreate() 中,我实例化了 ArrayList 的一个新实例。然后,我将该 ArrayList 赋予 ArrayAdapter 的新实例,并使用该 ArrayAdapter 在 ListView 上调用 setAdapter。 onCreate() 之后,onRestoreInstanceState() 被调用。在 onRestoreInstanceState() 中,我基本上将 ArrayList 设置为一个新的 ArrayList(从 InstanceState 读取的)。即使我随后将 ArrayAdapter 的项目变量指向这个新的 ArrayList,ArrayAdapter 似乎仍然指向旧的空变量。因此,列表中没有显示任何内容。我更改了 onRestoreInstanceState 将从 InstanceState 读取的 ArrayList 复制到 onCreate() 中已声明的 ArrayList。

I have a ListView that uses an ArrayAdapter. The items in the ArrayAdapter are a custom object I've created.

I've extended this custom object from Parcelable. When the user rotates the screen and the instanceState is saved, it writes the ArrayList to the outState. When the ListActivity is recreated, the instanceState is read back in to the ArrayList. In onResume(), I then assign the ArrayList to the ArrayAdapter's items variable, and then call setAdapter on the ListView with the ArrayAdapter.

Unfortunately, nothing shows up in the ListView.

I've debugged through and everything seems to be working perfectly fine until setAdapter is called. The ArrayList is read back in from the instanceState, all the data is there and preserved perfectly.

Any ideas?

Update:

I've fixed the issue. I'll try to write an explanation here until stackoverflow lets me create an answer.

In onCreate(), I instantiate a new instance of my ArrayList. I then give that ArrayList to a new instance of my ArrayAdapter and call setAdapter on the ListView with this ArrayAdapter. After onCreate(), onRestoreInstanceState() gets called. In onRestoreInstanceState(), I essentially set my ArrayList to a new ArrayList (the one read from the InstanceState). Even though I then point my ArrayAdapter's item variable at this new ArrayList, the ArrayAdapter seems to still be pointing at the old empty one. Thus, nothing shows up in the list. I've altered onRestoreInstanceState to copy the ArrayList read from the InstanceState to the already-declared ArrayList from onCreate().

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柠檬 2024-12-02 04:26:31

在 onCreate() 中,我实例化了 ArrayList 的一个新实例。然后,我将该 ArrayList 赋予 ArrayAdapter 的新实例,并使用该 ArrayAdapter 在 ListView 上调用 setAdapter。 onCreate() 之后,onRestoreInstanceState() 被调用。在 onRestoreInstanceState() 中,我基本上将 ArrayList 设置为一个新的 ArrayList(从 InstanceState 读取的)。即使我随后将 ArrayAdapter 的项目变量指向这个新的 ArrayList,ArrayAdapter 似乎仍然指向旧的空变量。因此,列表中没有显示任何内容。我更改了 onRestoreInstanceState 将从 InstanceState 读取的 ArrayList 复制到 onCreate() 中已声明的 ArrayList。

In onCreate(), I instantiate a new instance of my ArrayList. I then give that ArrayList to a new instance of my ArrayAdapter and call setAdapter on the ListView with this ArrayAdapter. After onCreate(), onRestoreInstanceState() gets called. In onRestoreInstanceState(), I essentially set my ArrayList to a new ArrayList (the one read from the InstanceState). Even though I then point my ArrayAdapter's item variable at this new ArrayList, the ArrayAdapter seems to still be pointing at the old empty one. Thus, nothing shows up in the list. I've altered onRestoreInstanceState to copy the ArrayList read from the InstanceState to the already-declared ArrayList from onCreate().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文