Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

Custom Filter problem

$
0
0

I'm trying to code a custom filter for my custom adapter, but i'm stuck because even though i'm implementing IFilterable in the adapter class, xamarin tells me that he can't find a method to override Filter Filter.

I post some code here so that you can help me out.
The adapter:

    public class ListNewsAdapter : BaseAdapter, IFilterable
    {
        Context context;
        public List<News> listNews;
        LayoutInflater inflater;
        Filter titleFilter;

        public ListNewsAdapter (Context context, List<News> listNews) : base(context, listNews)
        {
            this.listNews = listNews;
            this.inflater = LayoutInflater.From (context);
            this.context = context;
            this.titleFilter = new TitleFilter ();
        }

        public override int Count {
            get { return listNews.Count; }
        }

        public override Java.Lang.Object GetItem(int position) {
            return null;
        }

        public override long GetItemId (int position) {
            return position;
        }

        public override View GetView(int position, View convertView, ViewGroup parent){
            //A lot of stuff
            return convertView;
        }

        public override Filter Filter {  //  <= There, this method gives me the error
            get{
                return titleFilter;
            }
        }
 }

The filter:

    public class TitleFilter : Filter{
        ListNewsAdapter customAdapter;
        public TitleFilter (ListNewsAdapter adapter) : base() {
            customAdapter = adapter;
        }


        protected override Filter.FilterResults PerformFiltering(Java.Lang.ICharSequence constraint){
            FilterResults results = new FilterResults ();
            if (constraint != null && constraint.Length > 0) {
                List<News> matchList = new List<News> ();
                foreach (News news in customAdapter.listNews) {
                    if (news.Title.ToUpper ().Contains (constraint.ToString ().ToUpper ())) {
                        matchList.Add (news);
                    }
                }

                Java.Lang.Object[] resultsValues;
                resultsValues = new Java.Lang.Object[matchList.Count];
                for (int i = 0; i < matchList.Count; i++) {
                    resultsValues[i] = matchList[i];
                }

                results.Count = matchList.Count;
                results.Values = resultsValues;
            }
            return results;
        }

        protected override void PublishResults (Java.Lang.ICharSequence constraint, Filter.FilterResults results)
        {
            List<News> filteredList = new List<News> ();
            for (int i = 0; i < ((Java.Lang.Object[])results.Values).Length; i++) {
                filteredList.Add ((Java.Lang.Object[])results.Values [i]);
            }
            customAdapter.listNews = filteredList;
            customAdapter.NotifyDataSetChanged();
        }
    }

I'm also not sure about the PublishResults method up here ^, this conversions back an forth to a java Object are driving me crazy..

Your help would be appreciated :)


Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>