Android - Dynamic Add Menu Item

摘要:Android - Dynamic Add Menu Item

這裡要,程式撈取Gateway資料後,動態在Menu加入MenuItem,並設定相關的Click動作


   public void addMenuItems(MenuItem mMenuTest) {
           final ArrayList arr = getServerMenuTitles();
	    
	    mMenuTest.getSubMenu().clear();
	    
	    for(String data:arr) {
	        MenuItem item = mMenuCity.getSubMenu().add(data);
	        item.setOnMenuItemClickListener(mTestClick);	        
	    }
   }

	public TestClick mTestClick = new TestClick();

	/**
	 * Test Menu Click Handler
	 */
	public class TestClick implements MenuItem.OnMenuItemClickListener {

		@Override
		public boolean onMenuItemClick(MenuItem item) {
                           //TO DO Some Thing
		}
	}