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
2
My activity page;
Cont;
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
2
My activity page;
Cont;
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.
It was works since 2 days ago but I dont know why now I have got two error code CS0534 and CS0115. 
Thank you so much for your effort ! This is worked for me, I hope useful for another people,
Have a good day!
Hello,
try to refresh the adapter -> adapter.NotifyDataSetChanged();
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.
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;
And this is my items in activity, but the search view cannot see the objects.
10 people are following this question.