我可以在 SearchView 中实现 Google 番石榴搜索吗
Can I implement Google Guava Search in SearchView
我在自定义列表中使用 SearchView。它显示错误的结果,即每次都显示相同的结果(列表的第一项)。
因此,我尝试使用 Google 的 Gauva 过滤器,当我在过滤后记录结果时它非常有效。但是现在我不知道如何将它与我的列表视图集成。
这是我的CustomList.java代码
包裹 com.jarvis.easysplay.adapter;
public class CustomList extends ArrayAdapter implements Filterable{
private String[] names;
private String[] desc;
private Integer[] imageid;
private Activity context;
public CustomList(Activity context, String[] names, String[] desc) {
super(context, R.layout.list_item, names);
this.context = context;
this.names = names;
this.desc = desc;
// this.imageid = imageid;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View listViewItem = inflater.inflate(R.layout.song_list_layout, null, true);
TextView textViewName = (TextView) listViewItem.findViewById(R.id.song_title);
TextView textViewDesc = (TextView) listViewItem.findViewById(R.id.song_author);
ImageView image = (ImageView) listViewItem.findViewById(R.id.imageView);
// TextView options = (TextView) listViewItem.findViewById(R.id.options);
//Set Data
textViewName.setText(names[position]);
textViewDesc.setText(desc[position]);
return listViewItem;
}
@NonNull
@Override
public Filter getFilter() {
return super.getFilter();
}
}
这是我的搜索过滤器代码:-
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.music_search, menu);
MenuItem searchItem = menu.findItem(R.id.music_search_bar);
SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setQueryHint("Search Song");
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
// SongFragment.this.customList.getFilter().filter(newText); //I used this but it is not working
List<String> list = new ArrayList<>();
list.addAll(arrayList);
List<String> filteredList = Lists.newArrayList(Collections2.filter(
list, Predicates.contains(Pattern.compile(newText,Pattern.CASE_INSENSITIVE))));
String[] stringArray = filteredList.toArray(new String[0]);
CustomList custom = new CustomList(getActivity(),stringArray,stringArray);
custom.getFilter().filter(newText);
Log.d("searchResult",filteredList.toString());
return false;
}
});
super.onCreateOptionsMenu(menu, inflater);
}
谁能告诉我如何在我的代码中使用 Google 的 Guava,或者如何在不使用 google 的 Guava 过滤器的情况下获得正确的结果。
我做了很多 google,也尝试了很多解决方案,但都没有用:(.
您正在创建一个新的 CustomList
适配器,但您没有对它执行任何操作。您需要在 ListView 或您正在使用的任何 AdapterView 中安装新的适配器。如果您使用的是 ListView,则可以调用 ListView.setAdapter(custom)
。您可能不需要混合使用 Filterable
过滤和 Guava 过滤。
我在自定义列表中使用 SearchView。它显示错误的结果,即每次都显示相同的结果(列表的第一项)。 因此,我尝试使用 Google 的 Gauva 过滤器,当我在过滤后记录结果时它非常有效。但是现在我不知道如何将它与我的列表视图集成。
这是我的CustomList.java代码 包裹 com.jarvis.easysplay.adapter;
public class CustomList extends ArrayAdapter implements Filterable{
private String[] names;
private String[] desc;
private Integer[] imageid;
private Activity context;
public CustomList(Activity context, String[] names, String[] desc) {
super(context, R.layout.list_item, names);
this.context = context;
this.names = names;
this.desc = desc;
// this.imageid = imageid;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View listViewItem = inflater.inflate(R.layout.song_list_layout, null, true);
TextView textViewName = (TextView) listViewItem.findViewById(R.id.song_title);
TextView textViewDesc = (TextView) listViewItem.findViewById(R.id.song_author);
ImageView image = (ImageView) listViewItem.findViewById(R.id.imageView);
// TextView options = (TextView) listViewItem.findViewById(R.id.options);
//Set Data
textViewName.setText(names[position]);
textViewDesc.setText(desc[position]);
return listViewItem;
}
@NonNull
@Override
public Filter getFilter() {
return super.getFilter();
}
}
这是我的搜索过滤器代码:-
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.music_search, menu);
MenuItem searchItem = menu.findItem(R.id.music_search_bar);
SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setQueryHint("Search Song");
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
// SongFragment.this.customList.getFilter().filter(newText); //I used this but it is not working
List<String> list = new ArrayList<>();
list.addAll(arrayList);
List<String> filteredList = Lists.newArrayList(Collections2.filter(
list, Predicates.contains(Pattern.compile(newText,Pattern.CASE_INSENSITIVE))));
String[] stringArray = filteredList.toArray(new String[0]);
CustomList custom = new CustomList(getActivity(),stringArray,stringArray);
custom.getFilter().filter(newText);
Log.d("searchResult",filteredList.toString());
return false;
}
});
super.onCreateOptionsMenu(menu, inflater);
}
谁能告诉我如何在我的代码中使用 Google 的 Guava,或者如何在不使用 google 的 Guava 过滤器的情况下获得正确的结果。 我做了很多 google,也尝试了很多解决方案,但都没有用:(.
您正在创建一个新的 CustomList
适配器,但您没有对它执行任何操作。您需要在 ListView 或您正在使用的任何 AdapterView 中安装新的适配器。如果您使用的是 ListView,则可以调用 ListView.setAdapter(custom)
。您可能不需要混合使用 Filterable
过滤和 Guava 过滤。