问题
I'm trying to use the SearchView Support v4 version with action bar sherlock.
So i have my search button in the action bar -> when i touch it the keyboard show up and the searchBar too.
My problem is that i need to use the listeners onQueryTextSubmit and onQueryTextChange but they are never fired. I need to use the searh query string and do custom stuff with it.
Here is the full activity.java
public class ActivityMain extends SherlockFragmentActivity implements OnQueryTextListener, DialogFragmentListener {
/**
* PRIVATE ATTRIBUTES
*/
private static final String TAG = "ActivityMain";
private ViewPager _viewPager;
private TabsAdapter _tabsAdapter;
private DialogFiltre _dialogFiltre;
private String _searchCurrentQuery;
// data
private boolean _doubleBackToExitPressedOnce = false;
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.clear();
switch ((int) _viewPager.getCurrentItem()) {
case 0:
getSupportMenuInflater().inflate(R.menu.empty_menu, menu);
break;
case 1:
getSupportMenuInflater().inflate(R.menu.action_bar_menu, menu);
break;
case 2:
getSupportMenuInflater().inflate(R.menu.empty_menu, menu);
break;
}
return super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.action_bar_menu, menu);
MenuItem searchItem = menu.findItem(R.id.search);
SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setSubmitButtonEnabled(true);
searchView.setOnQueryTextListener(queryTextListener);
return true;
}
@Override
public boolean onQueryTextSubmit(String query) {
Log.i(TAG, "onQueryTextSubmit--");
onSearchClicked(query);
// hide keyboard
InputMethodManager inputManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
Log.d(TAG, "onQueryTextChange--");
_searchCurrentQuery = newText.toString();
EtablissementApplication._adapter.getFilter().filter(_searchCurrentQuery);
return true;
}
private void onSearchClicked(String query) {
Log.d(TAG, "onSearchClicked--");
_searchCurrentQuery = query.toString();
EtablissementApplication._adapter.getFilter().filter(_searchCurrentQuery);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
break;
case R.id.search:
break;
case R.id.menu_filtre:
_dialogFiltre = DialogFiltre.newInstance(R.string.menu_filtre, this);
_dialogFiltre.setValidDialogListener(this);
_dialogFiltre.show(getSupportFragmentManager(), null);
break;
}
return super.onOptionsItemSelected(item);
}
回答1:
You are trying to use a new SearchView instead of using the one created by the SupportMenuInflater. You setting the listener to different SearchView that you see on the screen. Also, every time onPrepareOptionsMenu is called, new SearchView is created, and thus it has no listeners set.
Try to do onPrepareOptionsMenu like this:
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.clear();
switch ((int) _viewPager.getCurrentItem()) {
case 0:
getSupportMenuInflater().inflate(R.menu.empty_menu, menu);
break;
case 1:
getSupportMenuInflater().inflate(R.menu.action_bar_menu, menu);
MenuItem searchItem = menu.findItem(R.id.search);
SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setSubmitButtonEnabled(true);
searchView.setOnQueryTextListener(queryTextListener);
break;
case 2:
getSupportMenuInflater().inflate(R.menu.empty_menu, menu);
break;
}
return super.onPrepareOptionsMenu(menu);
}
And remove the overridden method
@Override
public boolean onCreateOptionsMenu(Menu menu) { }
回答2:
You may have to set searchView.setOnQueryTextListener(this);
on your SearchView reference.
来源:https://stackoverflow.com/questions/16854212/android-abs-with-searchview-onquerytextlistener-not-working