Android ListView/Adapter - 更新 ArrayList 时出现 IllegalStateException

发布于 2024-10-30 05:17:13 字数 1116 浏览 1 评论 0原文

首先,我浏览了网站,除了“隐藏 ListView”之外,找不到解决我的问题的方法。对于我来说这不是一个可行的解决方案!

所以,问题是: 我创建了自己的“EndlessAdapter”,它允许我填充 ArrayList<>当用户滚动到列表底部时,为 ListView 提供更多数据。这一切都很完美,除非用户在列表更新时选择行,或者在列表更新时更改方向(我想这可能是任何 UI 交互)。

我已将 mAdapter.notifyDatasetChanged() 方法包装在 try catch 中,但我似乎永远无法捕获此 IllegalStateException,所以我不知道它发生在哪里!

有没有办法,当适配器更新 ListView 时,我可以阻止用户与应用程序交互,直到应用程序完成为止? EndlessAdapter 过程的较长部分是新数据的下载。在我看来,更新 ListView 是一个快速的过程。

我真的需要解决这个问题。我不想发布源代码,但如果需要的话我可以尝试发布重要信息?否则,如果有这方面经验的人可以通过电子邮件或即时消息聊天,我将非常感激?

以下是堆栈跟踪:

04-05 21:11:35.363: ERROR/AndroidRuntime(25707): java.lang.IllegalStateException: 适配器的内容已更改,但 ListView 未收到通知。确保适配器的内容不是从后台线程修改的,而是仅从 UI 线程修改的。 [在 ListView(16908298, class android.widget.ListView) 和 Adapter(class com.mimminito.heatdroid.view.DealsView$DealAdapter)

下面是我的代码示例的链接: http://pastebin.com/wfmWSGwQ

我在 onCreate() 中创建适配器。然后,在解析 XML 后,我将适配器分配给 ListView。在同一方法中,我调用 updateList() 方法。这样就得到了已经下载的数据,然后通知mAdapter它的数据已经改变了(全部在UI线程上运行)

谢谢你的时间

Adam

First off, I have looked around the site and could not find a solution to my question, other than "hide the ListView". This is not a viable solution in my case!

So, the problem:
I have created my own "EndlessAdapter" which allows me to populate the ArrayList<> backing the ListView with more data as the user scrolls to the bottom of the List. This all works perfectly, except for when the user selects a Row whilst the List is updating, or changes orientation whilst the List is updating (I suppose it could be any UI interactions).

I have wrapped my mAdapter.notifyDatasetChanged() method in a try catch, but I can never seem to catch this IllegalStateException, so I do not know where is it occurring!

Is there a way, whilst the Adapter is updating the ListView, that I can block the user from interacting with the application until its finished? The long part of the EndlessAdapter process is the downloading of the new data. Updating the ListView is a fast process imo.

I really need to get this issue sorted. Id rather not post up source code, but if need be I can try and post up the vitals? Otherwise I would be very appreciative if someone with experience in this could have a chat over Email or IM?

Here is the stack trace:

04-05 21:11:35.363: ERROR/AndroidRuntime(25707): java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. [in ListView(16908298, class android.widget.ListView) with Adapter(class com.mimminito.heatdroid.view.DealsView$DealAdapter)

Here is a link to a sample of my code:
http://pastebin.com/wfmWSGwQ

I create the Adapter in the onCreate(). I then assign the Adapter to the ListView once the XML has been parsed. In the same method I call the updateList() method. This gets the data that has been downloaded, and then notifies the mAdapter that its data has been changed (all run on the UI thread)

Thanks for your time

Adam

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

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

发布评论

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

评论(1

你的笑 2024-11-06 05:17:13

您正在执行添加到列表和 UI 线程中的通知的操作吗?这是保证 UI 和其他线程不会同时使用该列表的唯一方法。

You're doing both the adding to the list and the notify in the UI-thread as it says? This is the only way to guarantee that the list is not used by the UI and your other threads at the same time.

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