简体   繁体   English

在显示它们时更改Android ContextMenu项目(ICS之前的版本)

[英]Changing Android ContextMenu items while they are displayed (pre ICS)

I'm trying to set ContextMenu item invisible ( item.setVisible(false) ) while it is already displayed (saved reference to the menu as a member during onCreateContextMenu() ). 我正在尝试将ContextMenu项目设置为不可见( item.setVisible(false) ),同时将其显示(在onCreateContextMenu()期间作为成员保存对菜单的引用)。

While doing this on 2.3 I get exception: 在2.3上执行此操作时,出现异常:

01-17 09:54:42.563: E/AndroidRuntime(2494): 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(16908823, class com.android.internal.app.AlertController$RecycleListView) with Adapter(class com.android.internal.view.menu.MenuBuilder$MenuAdapter)]

All my code is ran in UI thread of course. 我的所有代码当然都在UI线程中运行。

I guess that ContextMenu in 2.3 is implemented using ListView and blackened by corresponding `ListAdapterv. 我猜想2.3中的ContextMenu是使用ListView实现的,并被相应的`ListAdapterv涂黑了。 In ICS this exception is not thrown and the item becomes invisible as I've planned. 在ICS中,不会引发此异常,并且按照我的计划,该项目将变得不可见。

Anybody has an Idea how to do it on 2.3? 有人有一个想法如何在2.3上做到吗?

One workaround is not to emulate ContextMenu via Dialog , but I'd rather stick to ContextMenu 一种解决方法是不通过Dialog模拟ContextMenu ,但我宁愿坚持使用ContextMenu

You can try getting the Menu adapter by: 您可以尝试通过以下方式获取菜单适配器:

public boolean onContextItemSelected(MenuItem item) { AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo(); public boolean onContextItemSelected(MenuItem item){AdapterView.AdapterContextMenuInfo info =(AdapterView.AdapterContextMenuInfo)item.getMenuInfo();

// ... Do Something // ... 做一点事

    adapter.notifyDataSetChanged();
}
return super.onContextItemSelected(item);    

} }

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

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