Android的NULL menuInfo在onCreateContextMenu和onContextItemSelected只能与onListItemClick手动调用openContextMenu。长按作品 [英] Android NULL menuInfo in onCreateContextMenu and onContextItemSelected only with manual call to openContextMenu in onListItemClick. Long click works
问题描述
我已经经历了很多这里的职位分析,并没有发现过像我的问题。
基本上我试图调用 openContextMenu(L)
在 onListItemClick
。这样做可以创建一个没有 menuInfo
的上下文菜单。执行长按会正常工作。执行长按之后,我的code将开始工作,并实际获得 menuInfo
不为null。
我有一个 ListActivity
是充满了 SimpleCursorAdapter
从抓取数据的SQL
。
在我的onCreate我 registerForContextMenu(getListView())
。
我一直在使用也尝试 registerForContextMenu(L)
之前的 openContextMenu(L)
电话。
任何帮助将是AP preciated!谢谢你。
下面是我的code的样本:
公共类MY_Activity扩展ListActivity {...@覆盖
公共无效的onCreate(捆绑savedInstanceState){ ... UpdateTable();
registerForContextMenu(getListView());
}...@覆盖
公共无效onListItemClick(ListView中升,观景,INT位置,长的id){
super.onListItemClick(L,视图,位置ID); //这并不工作,除非长时间点击先发生
// registerForContextMenu(升); //尝试在这里做太
openContextMenu(升);
// unregisterForContextMenu(升); //然后注销这里...
}@覆盖
公共无效onCreateContextMenu(文本菜单菜单视图V,ContextMenuInfo menuInfo){
super.onCreateContextMenu(菜单,V,menuInfo); // menuInfo将为空在这里。 menu.setHeaderTitle(上下文菜单);
menu.add(0,v.getId(),0,1);
menu.add(0,v.getId(),0,二);
menu.add(0,v.getId(),0,三);
}@覆盖
公共布尔onContextItemSelected(菜单项项){
AdapterContextMenuInfo信息=(AdapterContextMenuInfo)item.getMenuInfo();
如果(资讯== NULL){
Log.e(,空上下文菜单intem信息...);
返回false;
}
}公共无效UpdateTable(){
光标= DatabaseHelper_Main.GetCursor(添加my_id);
CursorAdapter的=新SimpleCursorAdapter(这一点,R.layout.my_listview_entry,
光标,字段,fieldResources,0);
setListAdapter(CursorAdapter的);
}...
今天我有一个非常类似的问题,并修复出乎意料地简单,但我不明白为什么,但我会在这里反正张贴。
@覆盖
公共无效onItemClick(适配器视图<>母公司,观景,INT位置,长的id){
m_contextMenu = TRUE;
registerForContextMenu(父);
m_listView.showContextMenuForChild(视图);
unregisterForContextMenu(父);
m_contextMenu = FALSE;
}
我用m_contextMenu布尔值指示正在显示的上下文菜单中,我有一个返回false m_contextMenu这样的上下文菜单不显示(如果onItemLongClick()返回true,它不会显示是真实的onItemLongClickListener上下文菜单)。
I have parsed through a lot of the posts here and haven't found anything quite like my problem.
Basically I am trying to call openContextMenu(l)
in onListItemClick
. Doing so creates a context menu with no menuInfo
. Performing a long click will work correctly. After the long click is performed, my code will start working and actually get a menuInfo
that is not null.
I have a ListActivity
that is filled with a SimpleCursorAdapter
which grabs data from SQL
.
In my onCreate I registerForContextMenu(getListView())
.
I have also tried using registerForContextMenu(l)
just before the openContextMenu(l)
call.
Any help would be appreciated! Thanks.
Here is a sample of my code:
public class MY_Activity extends ListActivity {
...
@Override
public void onCreate(Bundle savedInstanceState) {
...
UpdateTable();
registerForContextMenu(getListView());
}
...
@Override
public void onListItemClick(ListView l, View view, int position, long id) {
super.onListItemClick(l, view, position, id);
//THIS DOESNT WORK UNLESS A LONG CLICK HAPPENS FIRST
//registerForContextMenu(l); //Tried doing it here too
openContextMenu(l);
//unregisterForContextMenu(l); //Then unregistering here...
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
//menuInfo will be null here.
menu.setHeaderTitle("Context Menu");
menu.add(0, v.getId(), 0, "One");
menu.add(0, v.getId(), 0, "Two");
menu.add(0, v.getId(), 0, "Three");
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
if(info == null) {
Log.e("","NULL context menu intem info...");
return false;
}
}
public void UpdateTable() {
cursor = DatabaseHelper_Main.GetCursor(my_id);
cursorAdapter = new SimpleCursorAdapter(this, R.layout.my_listview_entry,
cursor, fields, fieldResources, 0);
setListAdapter(cursorAdapter);
}
...
I had a very similar issue today, and the fix was unexpectedly simple but I don't understand why, but I'll post it here anyway.
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
m_contextMenu = true;
registerForContextMenu(parent);
m_listView.showContextMenuForChild(view);
unregisterForContextMenu(parent);
m_contextMenu = false;
}
I use the m_contextMenu boolean to indicate that the context menu is being shown, I have an onItemLongClickListener that returns false if m_contextMenu is true so that the context menu does show up (if onItemLongClick() returns true, it won't show the context menu).
这篇关于Android的NULL menuInfo在onCreateContextMenu和onContextItemSelected只能与onListItemClick手动调用openContextMenu。长按作品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!