2017-06-26 3 views
1

ich meinen Adapter filtern möge. Ich habe einen RecyclerView, der diesen Adapter aufstellt. Ich staple mit, wie man meinen Adapter filtert. Ich habe nicht das Schlüsselwort filter für meinen Adapter. Mit dem Tutorial i folgenden bin, filtert es den Adapter in diesem Format adapter.filter.InvokeFilter aber ich bekomme ein Fehler, das zu tun.Filteradapter mit Suche - Xamarin.Droid

, was ich verpasst?

PS Ich habe bereits mein Adapter einrichten und zeigt Daten recyclerView mit

Aktivität

public override bool OnCreateOptionsMenu(IMenu menu) 
     { 

      MenuInflater.Inflate(Resource.Menu.top_menus, menu); 

      var item = menu.FindItem(Resource.Id.search); 

      var searchview = (Android.Support.V7.Widget.SearchView)MenuItemCompat.GetActionView(item); 

      searchview.SetOnQueryTextListener(this); 

      return base.OnCreateOptionsMenu(menu); 
     } 


public bool OnQueryTextChange(string newText) 
     { 
      //this is where i get the error 
      adapter.filter 
     } 

     public bool OnQueryTextSubmit(string query) 
     { 
      throw new NotImplementedException(); 

     } 

Adapter

public class CountryAdapter: RecyclerView.Adapter 
    { 
     private JavaList<Country> country; 
     private Context context; 
     public event EventHandler<int> ItemClick; 
     public CountryActivity nation; 



     public CountryAdapter(JavaList<Country> country, Context context) 
     { 
      this.country =country; 
      this.context = context; 
      nation = (CountryActivity)context; 

     } 




     public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position) 
     { 
      CountryHolder hold = holder as CountryHolder; 

     } 

     public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) 
     { 
      View v = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.List_Item, parent, false); 
      CountryHolder holder = new CountryHolder (v, OnClick,nation); 

      return holder;  
     } 

     public override int ItemCount { 

      get {return nation.Size(); } 

     } 


     void OnClick(int position) 
     { 
      if (ItemClick != null) 
       ItemClick(this, position); 
     } 

     } 
+0

Wahrscheinlich wurde er Filterung 'ListView' – Yupi

+0

Dieses Tutorial hilft mir viel vielleicht können Sie helfen auch https://www.survivingwithandroid.com/2012/10/android-listview-custom-filter-and.html – Ashky

+0

@Yupi, Ja ich gerade überprüft und er arbeitete Listview. Gibt es überhaupt für RecyclerView? – LearnXamarin

Antwort

0

Ich bin nicht sicher, Wille diese Arbeit, aber ich hoffe, es wird dir helfen. Versuchen Sie, eine Methode in Ihrem Adapter-Klasse zu erstellen, wie:

public void Filter(JavaList<Country> countries) 
{ 
    country = new JavaList<>(); 
    country.addAll(countries); 
    notifyDataSetChanged(); 
} 

Dann in Ihrem Activity wo Sie versuchen Suche Preform:

public bool OnQueryTextChange(string newText) 
    { 
     newText = newText.ToLowerCase(); 
     JavaList<Country> countriesList = new JavaList<>(); 
     foreach(Country country : countries) // data which you used for populating adapter 
     { 
      String name = country.getName.ToLowerCase(); // I don't know how your Country class code is setup I suppose you have getters and setters like name for countries 
     if(name.Contains(newText)) 
      countriesList.add(country); 
     } 
    countyadapter.Filter(countriesList); 
    return true; 
    }