ActionBarSherlock requesting search

I`m trying to request search from ActionBarSherlock.

My class extends SherlockListActivity.

This is how I`m adding Menu button to the ActionBar:

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add("Save")
            .setIcon(R.drawable.ic_action_search)
            .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM |
                MenuItem.SHOW_AS_ACTION_WITH_TEXT);
        return true;
    }

The problem is that I am unable to invoke it by id:

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch(item.getItemId())  {
            case ?????: {
                onSearchRequested();
                return true;
            }
        }
        return true;
    }

How people usually solve this problem?

Thanks.

Answers


If you inflate you menu from an XML menu file you will set an id which you can then use in your onOptionsItemSelected method. Like this:

XML

<menu xmlns:android="http://schemas.android.com/apk/res/android">     
    <item android:id="@+id/menu_search"
    android:icon="@drawable/ic_menu_search"
    android:showAsAction="ifRoom"
    android:actionViewClass="android.widget.SearchView" android:title="@string/search"/>
</menu>

Fragment

 @Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {

        case R.id.menu_search:
            onSearchRequested();

            return true;

        default:
            return super.onOptionsItemSelected(item);
    }
}

Also you can use

 @Override
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {

    menu.add(0, 1, 1, "Refresh").setIcon(R.drawable.ic_refresh).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
    menu.add(0, 2, 2, "Search").setIcon(R.drawable.ic_search).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

    return super.onCreateOptionsMenu(menu);
}


@Override
    public boolean onOptionsItemSelected(com.actionbarsherlock.view.MenuItem item) {
        switch (item.getItemId()){
            case 1:
                ...
                break;
            case 2:
                ...
                break;
        }
        return true;
    }

Need Your Help

Syntax error in SQL with NEW values

sql triggers

CREATE OR REPLACE TRIGGER precio_reparaciones

Drawing with mouse on the GUI in matlab

matlab matlab-guide

I want to have a program in matlab with GUI, at run the program, user can draw anythings with mouse on the axes in GUI, and i want to saving created image in a matrix. how can i to do this?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.