i am using a google guide to create place suggestions in my Android app made with Xamarin. I was wondering how would you convert this Java code to c#, and how to deal with the methods inside a method in c#? Thanks!
public Filter getFilter() {
Filter filter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults filterResults = new FilterResults();
if (constraint != null) {
// Retrieve the autocomplete results.
resultList = autocomplete(constraint.toString());
// Assign the data to the FilterResults
filterResults.values = resultList;
filterResults.count = resultList.size();
}
return filterResults;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if (results != null && results.count > 0) {
notifyDataSetChanged();
}
else {
notifyDataSetInvalidated();
}
}};
return filter;
}
Just create a new named class extending or implementing Filter
(depending on whether it's an interface or a class). Make it a nested class if you don't need it elsewhere.
For single-method interfaces, normally a delegate is the simplest way of representing the type - an idiom which Xamarin uses, I believe - and then you can use lambda expressions or anonymous methods to write the implementation "inline".
See more on this question at Stackoverflow