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.