2013-10-11 40 views
3

我已經解析了很多這裏的帖子,並沒有發現任何相當像我的問題。僅在手動調用onListItemClick中的openContextMenu時,才能在onCreateContextMenu和onContextItemSelected中選擇Android NULL menuInfo。長按工程

基本上我試圖撥openContextMenu(l)onListItemClick。這樣做會創建一個沒有menuInfo的上下文菜單。執行長按可以正常工作。在執行長按操作後,我的代碼將開始工作,並且實際上得到不爲空的menuInfo

我有一個ListActivity充滿了SimpleCursorAdapterSQL抓取數據。

在我的onCreate我registerForContextMenu(getListView())。 我也嘗試在撥打openContextMenu(l)之前使用registerForContextMenu(l)

任何幫助,將不勝感激!謝謝。

這裏是我的代碼示例:

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

... 

回答

0

我今天有一個非常類似的問題,並修復意外地簡單,但我不明白爲什麼,但我會在這裏反正它張貼。

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

我用m_contextMenu布爾以表示正在顯示的上下文菜單中,我有一個返回false m_contextMenu使上下文菜單便顯示出來(如果onItemLongClick()返回true是真實的onItemLongClickListener,它將不會顯示上下文菜單)。

相關問題