How do I access menu items?

后端 未结 2 1924
滥情空心
滥情空心 2020-12-10 12:05

So I\'m trying to grab my menu items off the ActionBar and set them into some variables to use later. Below is some basic test code that tries to set the variable during the

相关标签:
2条回答
  • 2020-12-10 12:53

    MenuItem.getItem(index) take index of the menu item instead of id of menu item so use MenuItem.findItem which take menu item id as:

     mRefresh = menu.findItem(R.id.refresh); //item id
      OR
     mRefresh = menu.getItem(0); //item index
    
    0 讨论(0)
  • 2020-12-10 12:58

    getItem(int) returns a MenuItem at a specific index, while findItem(int) returns a MenuItem corresponding to the resource ID that is given. Call findItem instead of getItem.

    http://developer.android.com/reference/android/view/Menu.html#findItem(int)

    0 讨论(0)
提交回复
热议问题