Android Programming Tutorials

(Romina) #1
Searching For Food

android:title="Settings"
android:icon="@drawable/ic_menu_preferences"
/>
</menu>

You will need a suitable icon, such as ic_menu_search.png from the Android


SDK.


Then, add the corresponding segment to onOptionsItemSelected() in


LunchList to handle our new item:


@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId()==R.id.add) {
startActivity(new Intent(LunchList.this, DetailForm.class));

return(true);
}
else if (item.getItemId()==R.id.prefs) {
startActivity(new Intent(this, EditPreferences.class));

return(true);
}
else if (item.getItemId()==R.id.search) {
onSearchRequested();

return(true);
}

return(super.onOptionsItemSelected(item));
}

This triggers onSearchRequested(), which tells Android that we would like to


conduct a search in this application.


Finally, in initList() in LunchList, we need to see if our activity was


launched via a search and, if so, use the search string entered by the user.


Here is a revised initList() implementation for LunchList that does just


that:


private void initList() {
if (model!=null) {
stopManagingCursor(model);
model.close();
}

300
Free download pdf