2017-08-20 19 views
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); 
    } 

谁能告诉我怎么可以使用谷歌的番石榴在我的代码或如何使用,以获得正确的结果,而无需使用谷歌的番石榴过滤。 我做了这么多的谷歌,也尝试了很多解决方案,但它没有工作:(。

回答

0

你正在创建一个新的CustomList适配器,但你没有做任何事情,你需要安装如果你使用的是ListView,你可以拨打ListView.setAdapter(custom)你可能不需要使用Filterable和Guava过滤来混合和匹配过滤器

+0

现在, m到处导致项目的位置1。我希望该项目的实际位置在前面的列表视图中。有没有办法做到这一点? –