How to distinguish two menu item clicks in ActionBarSherlock?
actionbarsherlock, android, android-actionbar
Solution
private static final int REFRESH = 1;
private static final int SEARCH = 2;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, REFRESH, 0, "Refresh")
.setIcon(R.drawable.ic_action_refresh)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
menu.add(0, SEARCH, 0, "Search")
.setIcon(R.drawable.ic_action_search)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case REFRESH:
// Do refresh
return true;
case SEARCH:
// Do search
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Problem
I have been working with ActionBarSherlock recently, and follwing various tutorials, I wrote this code to add items to Action bar ``` @Override public boolean onCreateOptionsMenu(Menu menu) { menu.add("Refresh") .setIcon(R.drawable.ic_action_refresh) .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); menu.add("Search")// Search .setIcon(R.drawable.ic_action_search) .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); return true; } ``` However, I dont know how to distinguish the two clicks. Although I did find out that you have to Override onOptionsItemSelected to handle the clicks and also that a switch statement can be used to distinguish between clicks, but most tutorials use item ids from thier xml menus. Since I am not creating menus in xml how can i distinguish the clicks without ids.