1
我在自定義列表中使用SearchView。它顯示錯誤的結果,即每次都有相同的結果(列表的第一項)。 因此,我嘗試使用谷歌的Gauva過濾器,當我在過濾器之後記錄結果時,它效果很好。但是現在我不知道如何將其與我的列表視圖集成。我可以在SearchView中實現谷歌Guava搜索
這裏是我的代碼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();
}
}
這是我的搜索過濾器代碼: -
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);
}
誰能告訴我怎麼可以使用谷歌的番石榴在我的代碼或如何使用,以獲得正確的結果,而無需使用谷歌的番石榴過濾。 我做了這麼多的谷歌,也嘗試了很多解決方案,但它沒有工作:(。
現在, m到處導致項目的位置1。我希望該項目的實際位置在前面的列表視圖中。有沒有辦法做到這一點? –