使用SearchView过滤器适配器 - Xamarin.Droid

时间:2017-06-26 20:23:41

标签: c# android xamarin xamarin.android

我想过滤我的适配器。我有一个recyclerView设置这个适配器。我是如何过滤我的适配器的堆栈。我的适配器似乎没有关键字filter。通过我正在关注的教程,它以这种格式adapter.filter.InvokeFilter过滤适配器,但我得到了 这样做的错误。

我错过了什么?

PS 我已经设置了我的适配器并使用recyclerView显示数据

活动

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();

        }

适配器

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);
        }

       }

1 个答案:

答案 0 :(得分:0)

我不确定这是否有效,但我希望它会对你有所帮助。尝试在适配器类中创建一个方法,如:

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

然后在Activity你想在哪里进行搜索:

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;
    }