Created
August 2, 2011 16:40
-
-
Save jondavidjohn/1120606 to your computer and use it in GitHub Desktop.
Custom ListView with Custom Filtering (Mono for Android)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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()); | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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