Skip to content

Instantly share code, notes, and snippets.

@jondavidjohn
Created August 2, 2011 16:40
Show Gist options
  • Select an option

  • Save jondavidjohn/1120606 to your computer and use it in GitHub Desktop.

Select an option

Save jondavidjohn/1120606 to your computer and use it in GitHub Desktop.
Custom ListView with Custom Filtering (Mono for Android)
// grab input field to filter with
EditText etFilter = FindViewById(Resource.Id.studentFilter);
// call custom adapter filter method on TextChange
etFilter.TextChanged += delegate(object sender, Android.Text.TextChangedEventArgs e)
{
adapter.filterStudents(e.Text.ToString());
};
internal class StudentListAdapter : BaseAdapter
{
private List orig_items;
private List items;
private StudentList outer_context;
public StudentListAdapter(Context context, List items)
{
this.orig_items = items;
this.items = items;
this.outer_context = (StudentList) context;
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
//inflate the custom view
LayoutInflater vi =
(LayoutInflater)outer_context.GetSystemService(Context.LayoutInflaterService);
View v = vi.Inflate(Resource.Layout.ItemStudent, null);
/*
TODO - Populate View
*/
return v;
}
/***
* Custom Basic Filtering Function
*/
public void filterStudents(string filter)
{
// Create new empty list to add matched elements to
IList filtered = new List();
// examine each element to build filtered list
// remember to always use your original items list
foreach (Student s in this.orig_items)
{
string name = s.ToString();
if (name.Contains(filter))
{
filtered.Add(s);
}
}
//set new (filterd) current list of items
this.items = filtered;
//notify ListView to Rebuild
NotifyDataSetChanged();
}
public override Student this[int position]
{
get { return this.items[position]; }
}
public override int Count
{
get { return items.Count; }
}
public override long GetItemId(int position)
{
return position;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment