2010-09-02 86 views
1
@Override 
    public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) { 

     super.onCreateContextMenu(menu, v, menuInfo); 
     menu.setHeaderTitle("Selection Options"); 
     menu.add(0, v.getId(), 0, "Remove"); 
    } 

我希望我的菜單說「刪除AAPL」如何在android中創建動態上下文菜單?

我會從我的陣列適配器字符串AAPL,但我不知道我怎樣才能從這種方法來訪問我的數組適配器指數。

回答

2

鑄造menuInfoAdapterView.AdapterContextMenuInfo對象。從那裏,您可以獲得長時間點擊的ListView中物品的positionid

+0

= AdapterContextMenuInfo(AdapterContextMenuInfo)menuInfo; //無法解決...你能告訴我如何正確投射它嗎? – 2010-09-02 23:34:37

+0

@Sheehan Alam:它不是'AdapterContextMenuInfo'。它是'AdapterView.AdapterContextMenuInfo'。看到這個示例項目:http://github.com/commonsguy/cw-android/tree/master/Database/Constants/ – CommonsWare 2010-09-02 23:45:02

+0

該示例僅適用於ContextContextItemSelected,我正在做類似的事情。如何爲onCreateContextMenu?我無法投出ContextMenuInfo ... – 2010-09-03 00:08:38

5

實施例,如果使用的是具有自定義對象列表視圖:

@Override 
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { 
    super.onCreateContextMenu(menu, v, menuInfo); 

    AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo; 
    MyObject obj = (MyObject) myListView.getItemAtPosition(info.position); 

    menu.setHeaderTitle("Selection Options"); 
    menu.add(0, v.getId(), 0, "Remove " + obj.name); 
}