gpt4 book ai didi

Android SearchView setOnQueryTextListener 不工作?

转载 作者:搜寻专家 更新时间:2023-11-01 08:37:10 24 4
gpt4 key购买 nike

我正在我的应用程序的操作栏中创建一个搜索选项。我显示了“搜索”图标,当我单击它时它会折叠,我可以输入查询。

当我的应用程序启动时,将调用 onCreateOptionsMenu。我打印一个测试语句来验证它,然后在该函数中设置一个 setOnQueryTextListener。但它不起作用。当我在搜索 View 中键入内容时,我尝试通过打印来测试它,但它不起作用。请帮我找出错误。还有一个用于展开和折叠的监听器,但它也不起作用。

@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
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>

最佳答案

我用过这个,但不知道为什么有效:

Android abs with SearchView, onQueryTextListener not working

 @Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
MenuItem item = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}

@Override
public boolean onQueryTextChange(String newText) {
Log.i("well", " this worked");
return false;
}
});
}

关于Android SearchView setOnQueryTextListener 不工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35802924/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com