I'm using SearchView in my with Custom List. It shows wrong result ie same result every time(First item of the list). So, I try to use Google's Gauva Filter and it works great when I log the result after filter. But now I don't know how to integrate it with my listview.
here is my code of CustomList.java package 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();
}
}
And this is my Search Filter Code:-
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);
}
Can anybody tell me how can I use Google's Guava in my code or how to get Correct Result by using without using google's Guava filter. I did so much google and also try many solutions but it didn't work :(.
You're creating a new CustomList
adapter, but you aren't doing anything with it. You'll need to install the new adapter in your ListView or whatever AdapterView you are using. If you're using a ListView, you could call ListView.setAdapter(custom)
. You probably don't need to mix and match filtering using Filterable
and filtering by Guava.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.