Android : SearchView Not call onOptionsItemSelected

on Sunday, November 2, 2014


"Error" Well Strange Other times I've done it and never tive this problem ...


My searchview not call onOptionsItemSelected if i put "appname:actionViewClass="android.support.v7.widget.SearchView" on menu.


Manifest



<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<activity
android:name=".DashBoardActivity"
android:label="">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>


dash_board.xml (menu) (if I retreat actionViewClass he calls(onOptionsItemSelected ), but if i put he dont call)



<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:appname="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.servic.DashBoardActivity" >


<item android:id="@+id/action_search"
android:icon="@drawable/ic_action_search"
android:title="@string/action_search"
appname:showAsAction="always"
appname:actionViewClass="android.support.v7.widget.SearchView"/>

<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="@string/action_settings"
appname:showAsAction="always"/>



</menu>


DashBoardActivity



@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.dash_board, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
mSearchView = (SearchView) MenuItemCompat.getActionView(searchItem);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.

int id = item.getItemId();
switch (id)
{
case R.id.action_search:
mSearchView
.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
ToastMensagem("Digite um endereço.");
} else {
BuscarEndereco(mSearchView.getQuery().toString());
}
}



});
break;
}


return super.onOptionsItemSelected(item);
}

0 comments:

Post a Comment