Xamarin.Android SearchView can not see the CustomListView

CastielTR 141 Reputation points
2021-05-12T13:29:14.46+00:00

I can't connect search view and Custom List, When I searching the screen is blind. How Can I to do fix this?(C#)

1
96052-1.png
2
96053-2.png
My activity page;
96062-3.png
Cont;
96023-4.png

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,261 questions
0 comments No comments
{count} votes

Accepted answer
  1. JessieZhang-MSFT 7,706 Reputation points Microsoft Vendor
    2021-05-17T10:23:22.033+00:00

    Hello,

    Welcome to our Microsoft Q&A platform!

    You should implement interface IFilterable for your CursorAdapter . I created a simple demo according to your code, it works properly on my side.

    You can refer to the following code:

    public class CursorAdapter:ArrayAdapter, IFilterable  
    {  
        public Context c;  
        public List<Player> players;  
        public List<Player> _originalData;  
    
        public int resource;  
        public LayoutInflater inflater;  
        public Filter filter { get; private set; }  
    
        public CursorAdapter(Context context,int resource,List<Player> objects):base(context, resource, objects) {  
    
            this.c = context;  
            this.resource = resource;  
            this.players = objects;  
            filter = new SuggestionsFilter(this);  
            this._originalData = objects;  
        }  
    
        public override int Count  
        {  
            get  
            {  
                return players.Count;  
            }  
        }  
    
        public void UpDateData(List<Player> temp)  
        {  
            players.Clear();  
            players.AddRange(temp);  
            NotifyDataSetChanged();  
        }  
    
    
        public override View GetView(int position, View convertView, ViewGroup parent)  
        {  
            View view = convertView; // re-use an existing view, if one is available  
            MyHolder holder;  
    
            if (view != null)  
            {  
                holder = view.Tag as MyHolder;  
                holder.NameTXT.Tag = position;  
            }  
            else  
            {  
                holder = new MyHolder();  
    
                LayoutInflater inflater = (LayoutInflater)c.GetSystemService(Context.LayoutInflaterService);  
                view = inflater.Inflate(resource, null);  
    
                holder.Img = view.FindViewById<ImageView>(Resource.Id.imageView1);  
    
                holder.NameTXT = view.FindViewById<TextView>(Resource.Id.mNameTxt);  
                holder.NameTXT.Tag = position;  
    
                view.Tag = holder;  
            }  
    
            Player player = players[position];  
            holder.NameTXT.Text = player.Name;  
            holder.Img.SetImageResource(player.Image);  
    
            return view;  
        }  
    
        class SuggestionsFilter : Filter  
        {  
            readonly CursorAdapter _adapter;  
            public SuggestionsFilter(CursorAdapter adapter) : base()  
            {  
                _adapter = adapter;  
            }  
    
            protected override FilterResults PerformFiltering(ICharSequence constraint)  
            {  
                var returnObj = new FilterResults();  
                var results = new List<Player>();  
                if (_adapter._originalData == null)  
                    _adapter._originalData = _adapter.players;  
    
                if (constraint == null) return returnObj;  
    
                if (_adapter._originalData != null && _adapter._originalData.Any())  
                {  
                    // Compare constraint to all names lowercased.   
                    // It they are contained they are added to results.  
                    results.AddRange(  
                        _adapter._originalData.Where(  
                            chemical => chemical.Name.ToLower().Contains(constraint.ToString())));  
                }  
    
                // Nasty piece of .NET to Java wrapping, be careful with this!  
                returnObj.Values = FromArray(results.Select(r => r.ToJavaObject()).ToArray());  
                returnObj.Count = results.Count;  
    
                constraint.Dispose();  
    
                return returnObj;  
            }  
    
            protected override void PublishResults(ICharSequence constraint, FilterResults results)  
            {  
                using (var values = results.Values)  
                    _adapter.players = values.ToArray<Java.Lang.Object>()  
                        .Select(r => r.ToNetObject<Player>()).ToList();  
    
                _adapter.NotifyDataSetChanged();  
    
                // Don't do this and see GREF counts rising  
                constraint.Dispose();  
                results.Dispose();  
            }  
        }  
    
        public class MyHolder : Java.Lang.Object  
        {  
            public TextView NameTXT { get; set; }  
            public ImageView Img { get; set; }  
        }  
    }  
    

    class ObjectExtensions:

    public class JavaHolder : Java.Lang.Object  
    {  
        public readonly object Instance;  
    
        public JavaHolder(object instance)  
        {  
            Instance = instance;  
        }  
    }  
    
    public static  class ObjectExtensions  
    {  
    
        public static TObject ToNetObject<TObject>(this Java.Lang.Object value)  
        {  
            if (value == null)  
                return default(TObject);  
    
            if (!(value is JavaHolder))  
                throw new InvalidOperationException("Unable to convert to .NET object. Only Java.Lang.Object created with .ToJavaObject() can be converted.");  
    
            TObject returnVal;  
            try { returnVal = (TObject)((JavaHolder)value).Instance; }  
            finally { value.Dispose(); }  
            return returnVal;  
        }  
    
        public static Java.Lang.Object ToJavaObject<TObject>(this TObject value)  
        {  
            if (Equals(value, default(TObject)) && !typeof(TObject).IsValueType)  
                return null;  
    
            var holder = new JavaHolder(value);  
    
            return holder;  
        }  
    }  
    

    Usage in activity:

         sv1.QueryTextChange += Sv1_QueryTextChange;  
        private void Sv1_QueryTextChange(object sender, SearchView.QueryTextChangeEventArgs e)  
        {  
            string word = e.NewText;  
            adapter.filter.InvokeFilter(word);  
        }  
    

    Best Regards,

    Jessie Zhang

    ---
    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. JessieZhang-MSFT 7,706 Reputation points Microsoft Vendor
    2021-05-13T07:14:16.153+00:00

    Hello,

    Welcome to our Microsoft Q&A platform!

    Try the following code,it works on my side.

    1.method QueryTextChange

        private void MSearchViewBranch_QueryTextChange(object sender,SearchView.QueryTextChangeEventArgs e)  
        {  
            string word = e.NewText;  
            FindInsideList(word);  
        }  
    

    2.Method FindInsideList:

        private void FindInsideList(string text)  
        {  
            if (text != null && text.Length > 0)  
            {  
                List<Photo> filteredList = new List<Photo>();  
                foreach (var itm in loadBranch)  
                {  
                    if (itm != null)  
                    {  
                        text = text.ToLower();  
    
                        if (itm.mCaption != null ? itm.mCaption.ToLower().Contains(text) : false)  
                        {  
                            filteredList.Add(itm);  
                        }  
                    }  
                }  
                mAdapter.UpDateData(filteredList);  
            }  
            else  
            {  
                mAdapter.UpDateData(loadBranch);  
            }  
        }  
    

    3.UpDateData is a method in my Adapter

        public void UpDateData(List<Photo> temp)  
        {  
            mPhotoAlbum = temp;  
            NotifyDataSetChanged();  
        }  
    

    Note:

    1. mAdapter is my adapter and Photo is my item model
      2.loadBranch is the data list filled into my RecyclerView .
       List<Photo> loadBranch = new List<Photo>();  
      
      mAdapter = new PhotoAlbumAdapter(loadBranch);  
      

    Best Regards,

    Jessie Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

  2. cristopheB 546 Reputation points
    2021-05-12T14:38:22.193+00:00

    Hello,

    try to refresh the adapter -> adapter.NotifyDataSetChanged();