I am making a search option in the Actionbar of my application. I have the Search icon displaying and it collapes when i click it and I can type in queries.
When my application starts, the onCreateOptionsMenu gets called. I print a test statement to verify it and then inside that function I set an setOnQueryTextListener. But it doesn't work. I tried testing it by printing when ever I type something in the searchview but it doesn't work. Please help me find the bug. Also there is an Listener for when it expands and collapes and that doesn't work either.
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.manageusersmenu, menu);
MenuItem item = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
Log.i("onCreateOptionsMenu: ", "I made it in here");
MenuItemCompat.setOnActionExpandListener(item,
new MenuItemCompat.OnActionExpandListener() {
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
mAdapter.setFilter(mUsers);
Log.i("ActionCollapse: ", "Collapsed");
return true;
}
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
Log.i("ActionExpand: ", "Expanded");
return true;
}
});
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
Log.i("on query: ", newText);
List<User> filteredModelList = filter(mUsers, newText);
for (int i = 0; i < filteredModelList.size(); i++) {
Log.i("user, ", filteredModelList.get(i).getFName().toString());
}
mAdapter.setFilter(filteredModelList);
return true;
}
private List<User> filter(List<User> users, String query) {
query = query.toLowerCase();
final List<User> filteredModelList = new ArrayList<>();
for (User u : users) {
final String text = u.getFName().toLowerCase();
if (text.contains(query)) {
filteredModelList.add(u);
}
}
return filteredModelList;
}
});
}
Menu file
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<!-- "Mark Favorite", should appear as action button if possible -->
<item
android:id="@+id/add_new_user"
android:icon="@drawable/ic_action_name"
android:title="@string/add_new_user"
app:showAsAction="ifRoom"/>
<item
android:id="@+id/action_search"
android:icon="@android:drawable/ic_menu_search"
android:title="action_search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always|collapseActionView" />
</menu>
答案 0 :(得分:3)
我用过这个并且不确定原因:
Android abs with SearchView, onQueryTextListener not working
Id Name StartDate EndDate
1 abc 20160216 20160217
**1 abc 20160301 20160303**