Android NULL menuInfo in onCreateContextMenu and onContextItemSelected only with manual call to openContextMenu in onListItemClick. Long click works

陌路散爱 提交于 2019-12-06 06:28:47

问题


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);
}

...

回答1:


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).



来源:https://stackoverflow.com/questions/19323756/android-null-menuinfo-in-oncreatecontextmenu-and-oncontextitemselected-only-with

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!