2012-01-16 44 views
0

我已經創建了一個對話框,在那個對話框中我有一個列表視圖。我爲該listView創建了一個上下文菜單。它顯示上下文菜單,但當我點擊該上下文菜單項時,它不起作用。如何創建列表視圖的上下文菜單

任何人都可以請幫我解決這個問題。

+2

提供您的代碼 – 2012-01-16 08:55:55

+0

您是否正確處理onContextItemSelected事件? – waqaslam 2012-01-16 09:19:09

回答

0

請檢查下面的代碼使用您可以創建上下文菜單列表視圖::

  1. 中創建功能

    @Override 
    public boolean onContextItemSelected(MenuItem item) { 
    
        int menuItemId = item.getItemId(); 
    
        //to find out the id 
        AdapterView.AdapterContextMenuInfo menuInfo = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo(); 
    
        int currentSelectionItemPosition = menuInfo.position; 
    
        //moving the cursor to the selected position in the list view 
        restaurantFavoritesCursor.moveToPosition(currentSelectionItemPosition); 
        int restaurantid = restaurantFavoritesCursor.getInt(idColumnIndex); 
    
        if(menuItemId == R.id.deleteFavoriteRestaurant) 
        { 
         //write the Action 
        } 
        return true; 
    } 
    
  2. 中的OnCreate或OnStart中

  3. 內註冊事件

registerForContextMenu(listviewobject);

嘗試上面的代碼,我希望它能工作。

相關問題