我遇到了一个 IllegalStateException 将底层列表更新为适配器(可能是 ArrayAdapter 或 BaseAdapter 的扩展,我不记得了)。我目前没有也不记得异常的文本,但它说了一些关于列表内容更改的影响,而适配器没有收到更改通知。
此列表/可能/从 UI 线程(主线程)以外的另一个线程更新。更新此列表(添加项目)后,我调用 notifyDataSetChanged。问题似乎是 Adapter 或附加到 Adapter 的 ListView 尝试在调用此方法之前更新自身。发生这种情况时,将抛出 IllegalStateException。
如果我在更新前将 ListView 的可见性设置为 GONE,然后再次设置为 VISIBLE,则不会发生错误。但这并不总是实用的。
我在某处读到你不能从另一个线程修改底层的 this——这似乎限制了 MVC 模式,就像这个特定的列表一样,我想从不同的线程添加项目。我假设只要调用 notifyDataSetChanged() 就安全了——在调用此方法之前,适配器不会重新访问基础 List,但事实似乎并非如此。
我想我想问的是,从 UI 以外的线程更新底层列表是否安全?此外,如果我想修改适配器内的数据,我是修改底层列表还是适配器本身(通过其 add() 等方法)。通过Adapter修改数据貌似不对。
我在另一个网站上看到了一个似乎和我有类似问题的人的帖子:http://osdir.com/ml/Android-Developers/2010-04/msg01199.html (这是我从中获取 Visibility.GONE 和 .VISIBLE 想法的地方)。
为了让您更好地了解我的特定问题,我将描述一下我的列表、适配器等的设置方式。
我有一个名为 Queue 的对象,其中包含一个 LinkedList。 Queue 扩展了 Observable,当通过它的方法将事物添加到它的内部列表时,我调用 setChanged() 和 notifyListeners()。此 Queue 对象可以从任意数量的线程中添加或删除项目。
我有一个包含适配器的“队列 View ” Activity 。此 Activity 在其 onCreate() 方法中向我的 Queue 对象注册了一个 Observer 监听器。在 Observer 的 update() 方法中,我调用了 Adapter 上的 notifyDataSetChanged()。
我添加了很多日志输出并确定当此 IllegalStateExcption 发生时我的 Observer 回调从未被调用。所以就好像 Adapter 在 Observer 有机会通知它的 Observers 之前就注意到了 List 的变化,并调用我的方法来通知 Adapter 内容已经改变。
所以我想我想问的是,这是装配适配器的好方法吗?这是一个问题,因为我正在从 UI 线程以外的线程更新适配器的内容吗?如果是这种情况,我可能会想到一个解决方案(在创建 Queue 对象时为 UI 线程提供一个 Handler,并使用该 Handler 进行所有 List 修改,但这似乎不合适)。
我意识到这是一个非常开放的帖子,但我对此有点迷茫,非常感谢对我所写内容的任何评论。
最佳答案
This List /may/ be updated from another thread other than the UI thread (main)
那行不通。
I read somewhere that you cannot modify the underlying this from another thread--this would seem to limit an MVC pattern, as with this particular List, I want to add items from different threads
MVC 与线程无关。
can it be safe to update the underlying List from threads other than the UI?
没有。其他线程可以触发对适配器的更新(例如,通过 post()),但更新本身必须在主应用程序线程上处理,对于当前附加到 ListView 的适配器。
Additionally, if I want to modify the data within an Adapter, do I modify the underlying List or the Adapter itself (via its add(), etc. methods). Modifying the data through the Adapter seems wrong.
您通过 Adapter 本身为 ArrayAdapter 修改您的 Adapter。您通过 CursorAdapter 的底层数据库/内容提供程序修改您的 Adapter。其他适配器可能有所不同。
I've an object named Queue that contains a LinkedList. Queue extends Observable, and when things are added to its internal list through its methods, I call setChanged() and notifyListeners().
您是否考虑过使用 LinkedBlockingQueue,而不是实现您自己的线程安全 Queue?
This Activity, in its onCreate() method, registers an Observer listener to my Queue object. In the Observer's update() method I call notifyDataSetChanged() on the Adapter.
Adapters 应该自己调用 notifyDataSetChanged()(如果更改是由它们进行的)或者由正在更改数据的实体(例如, Cursor 用于 CursorAdapter)。 那就是MVC。 Activity 既不知道也不关心您的数据模型何时更改。
So it's as if the Adapter noticed the List's change before the Observer had a chance to notify its Observers, and call my method to notify the Adapter that the contents had changed.
可能您正在使用 ArrayAdapter,在这种情况下,所有这些额外的观察者/通知内容都会妨碍您,因为它已为您处理。您只需安排在主应用程序线程上更新 ArrayAdapter。
So I suppose what I'm asking is, is this a good way to rig-up an Adapter?
不是特别重要,恕我直言。
Is this a problem because I'm updating the Adapter's contents from a thread other than the UI thread?
如果您不强制将更新返回到主应用程序线程,一旦您解决了其他问题,这最终会崩溃。
give the Queue object a Handler to the UI thread when it's created, and make all List modifications using that Handler, but this seems improper
您可以使用 Handler,或者您可以在附加的 ListView 上调用 post()。
即兴创作,我将创建一个名为 ThreadSafeArrayAdapter 的 ArrayAdapter 子类,并用它代替 Queue。 ThreadSafeArrayAdapter 将 add()、insert() 和 remove() 替换为让父类(super class)执行其操作的那些通过 Handler 或 post() 在主应用程序线程上处理。
关于android - 更新适配器底层数据的最佳实践方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2707370/
我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
很好奇,就使用rubyonrails自动化单元测试而言,你们正在做什么?您是否创建了一个脚本来在cron中运行rake作业并将结果邮寄给您?git中的预提交Hook?只是手动调用?我完全理解测试,但想知道在错误发生之前捕获错误的最佳实践是什么。让我们理所当然地认为测试本身是完美无缺的,并且可以正常工作。下一步是什么以确保他们在正确的时间将可能有害的结果传达给您? 最佳答案 不确定您到底想听什么,但是有几个级别的自动代码库控制:在处理某项功能时,您可以使用类似autotest的内容获得关于哪些有效,哪些无效的即时反馈。要确保您的提
我正在尝试设置一个puppet节点,但rubygems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由rubygems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby
给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru
我有一个模型:classItem项目有一个属性“商店”基于存储的值,我希望Item对象对特定方法具有不同的行为。Rails中是否有针对此的通用设计模式?如果方法中没有大的if-else语句,这是如何干净利落地完成的? 最佳答案 通常通过Single-TableInheritance. 关于ruby-on-rails-Rails-子类化模型的设计模式是什么?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.co
我想了解Ruby方法methods()是如何工作的。我尝试使用“ruby方法”在Google上搜索,但这不是我需要的。我也看过ruby-doc.org,但我没有找到这种方法。你能详细解释一下它是如何工作的或者给我一个链接吗?更新我用methods()方法做了实验,得到了这样的结果:'labrat'代码classFirstdeffirst_instance_mymethodenddefself.first_class_mymethodendendclassSecond使用类#returnsavailablemethodslistforclassandancestorsputsSeco
我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i