question

CastielTR-4177 avatar image
0 Votes"
CastielTR-4177 asked YonglunLiu-MSFT edited

Xamarin.Android SearchView can not see the CustomListView

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


dotnet-xamarin
1.png (87.7 KiB)
2.png (89.2 KiB)
3.png (34.9 KiB)
4.png (14.6 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

JessieZhang-2116 avatar image
1 Vote"
JessieZhang-2116 answered CastielTR-4177 commented

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.


· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @CastielTR-4177 , have you resolved your problem?

0 Votes 0 ·

It was works since 2 days ago but I dont know why now I have got two error code CS0534 and CS0115. 98178-last.png


0 Votes 0 ·
last.png (63.4 KiB)

Did you change any code before this error occurred?

1 Vote 1 ·
Show more comments

Thank you so much for your effort ! This is worked for me, I hope useful for another people,
Have a good day!

0 Votes 0 ·
chbernard avatar image
0 Votes"
chbernard answered CastielTR-4177 commented

Hello,

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

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

It is not working unfortunately.

0 Votes 0 ·
JessieZhang-2116 avatar image
1 Vote"
JessieZhang-2116 answered JessieZhang-2116 commented

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.



· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Unfortunately not working because my custom adapter class is different, so how can I the use searchview is in my project ? please check the picture.
This is my custom adapter class;
96885-1.png

And this is my items in activity, but the search view cannot see the objects.
96895-2.png


0 Votes 0 ·
1.png (55.3 KiB)
2.png (16.1 KiB)

I have posted a new answer.You can check it.

0 Votes 0 ·