-
-
Save jeremejazz/103bbfb1e3ec0f67f8aefe04cd5139ad to your computer and use it in GitHub Desktop.
Revisions
-
Cheesebaron revised this gist
Mar 30, 2014 . 4 changed files with 118 additions and 122 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,7 +1,9 @@ namespace SearchViewSample { public class Chemical { public string Name { get; set; } public int DrawableId { get; set; } } } 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 charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,4 @@ using System.Collections.Generic; using System.Linq; using Android.App; using Android.Views; @@ -9,58 +8,35 @@ namespace SearchViewSample { public class ChemicalsAdapter : BaseAdapter<Chemical>, IFilterable { private List<Chemical> _originalData; private List<Chemical> _items; private readonly Activity _context; public ChemicalsAdapter(Activity activity, IEnumerable<Chemical> chemicals) { _items = chemicals.OrderBy(s => s.Name).ToList(); _context = activity; Filter = new ChemicalFilter(this); } public override long GetItemId(int position) { return position; } public override View GetView(int position, View convertView, ViewGroup parent) { var view = convertView ?? _context.LayoutInflater.Inflate(Resource.Layout.Chemical, null); var chemical = _items[position]; var nameView = view.FindViewById<TextView>(Resource.Id.chemName); var imageView = view.FindViewById<ImageView>(Resource.Id.chemImage); nameView.Text = chemical.Name; imageView.SetImageResource(chemical.DrawableId); return view; } @@ -75,31 +51,12 @@ public override Chemical this[int position] get { return _items[position]; } } public Filter Filter { get; private set; } public override void NotifyDataSetChanged() { // If you are using cool stuff like sections // remember to update the indices here! base.NotifyDataSetChanged(); } @@ -122,21 +79,33 @@ protected override FilterResults PerformFiltering(ICharSequence constraint) 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._items = values.ToArray<Object>() .Select(r => r.ToNetObject<Chemical>()).ToList(); _adapter.NotifyDataSetChanged(); // Don't do this and see GREF counts rising constraint.Dispose(); results.Dispose(); } } } 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 charactersOriginal file line number Diff line number Diff line change @@ -1,67 +0,0 @@ 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,92 @@ using System.Collections.Generic; using Android.App; using Android.Runtime; using Android.Support.V4.View; using Android.Support.V7.App; using Android.Support.V7.Widget; using Android.Views; using Android.Widget; using Android.OS; namespace SearchViewSample { [Activity(Label = "SearchView Sample", MainLauncher = true, Icon = "@drawable/icon", Theme = "@style/Theme.AppCompat.Light")] public class SearchViewActivity : ActionBarActivity { private SearchView _searchView; private ListView _listView; private ChemicalsAdapter _adapter; protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); SetContentView(Resource.Layout.Main); SupportActionBar.SetDisplayShowHomeEnabled(true); var chemicals = new List<Chemical> { new Chemical {Name = "Niacin", DrawableId = Resource.Drawable.Icon}, new Chemical {Name = "Biotin", DrawableId = Resource.Drawable.Icon}, new Chemical {Name = "Chromichlorid", DrawableId = Resource.Drawable.Icon}, new Chemical {Name = "Natriumselenit", DrawableId = Resource.Drawable.Icon}, new Chemical {Name = "Manganosulfate", DrawableId = Resource.Drawable.Icon}, new Chemical {Name = "Natriummolybdate", DrawableId = Resource.Drawable.Icon}, new Chemical {Name = "Ergocalciferol", DrawableId = Resource.Drawable.Icon}, new Chemical {Name = "Cyanocobalamin", DrawableId = Resource.Drawable.Icon}, }; _listView = FindViewById<ListView>(Resource.Id.listView); _adapter = new ChemicalsAdapter(this, chemicals); _listView.Adapter = _adapter; } public override bool OnCreateOptionsMenu(IMenu menu) { MenuInflater.Inflate(Resource.Menu.main, menu); var item = menu.FindItem(Resource.Id.action_search); var searchView = MenuItemCompat.GetActionView(item); _searchView = searchView.JavaCast<SearchView>(); _searchView.QueryTextChange += (s, e) => _adapter.Filter.InvokeFilter(e.NewText); _searchView.QueryTextSubmit += (s, e) => { // Handle enter/search button on keyboard here Toast.MakeText(this, "Searched for: " + e.Query, ToastLength.Short).Show(); e.Handled = true; }; MenuItemCompat.SetOnActionExpandListener(item, new SearchViewExpandListener(_adapter)); return true; } private class SearchViewExpandListener : Java.Lang.Object, MenuItemCompat.IOnActionExpandListener { private readonly IFilterable _adapter; public SearchViewExpandListener(IFilterable adapter) { _adapter = adapter; } public bool OnMenuItemActionCollapse(IMenuItem item) { _adapter.Filter.InvokeFilter(""); return true; } public bool OnMenuItemActionExpand(IMenuItem item) { return true; } } } } -
Cheesebaron created this gist
Mar 28, 2014 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,7 @@ namespace SearchViewSample { public class Chemical : Java.Lang.Object { public string Name { get; set; } } } 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,143 @@ using System.Collections.Generic; using System.Globalization; using System.Linq; using Android.App; using Android.Views; using Android.Widget; using Java.Lang; using Object = Java.Lang.Object; namespace SearchViewSample { public class ChemicalsAdapter : BaseAdapter<Chemical>, ISectionIndexer, IFilterable { private List<Chemical> _originalData; private List<Chemical> _items; private readonly Activity _context; private Dictionary<string, int> _alphaIndex; private string[] _sections; private Object[] _sectionsObjects; public ChemicalsAdapter(Activity activity, IEnumerable<Chemical> chemicals) { _items = chemicals.OrderBy(s => s.Name).ToList(); _context = activity; // Setup the indexer support SetupIndex(); Filter = new ChemicalFilter(this); } void SetupIndex() { _alphaIndex = new Dictionary<string, int>(); for (var i = 0; i < _items.Count; i++) { // loop through items var key = _items[i].ToString(); if (!_alphaIndex.ContainsKey(key)) _alphaIndex.Add(key, i); // add each 'new' letter to the index } _sections = new string[_alphaIndex.Keys.Count]; _alphaIndex.Keys.CopyTo(_sections, 0); // convert letters list to string[] // Interface requires a Java.Lang.Object[], so we create one here _sectionsObjects = new Object[_sections.Length]; for (var i = 0; i < _sections.Length; i++) { _sectionsObjects[i] = new String(_sections[i]); } } public override long GetItemId(int position) { return position; } public override View GetView(int position, View convertView, ViewGroup parent) { var view = convertView ?? _context.LayoutInflater.Inflate(Android.Resource.Layout.SimpleListItem1, null); var chemical = _items[position]; var nameView = view.FindViewById<TextView>(Android.Resource.Id.Text1); nameView.Text = chemical.Name; return view; } public override int Count { get { return _items.Count; } } public override Chemical this[int position] { get { return _items[position]; } } public int GetPositionForSection(int section) { return _alphaIndex[_sections[section]]; } public int GetSectionForPosition(int position) { var prevSection = 0; for (var i = 0; i < _sections.Length; i++) { if (GetPositionForSection(i) <= position || prevSection > position) continue; prevSection = i; break; } return prevSection; } public Object[] GetSections() { return _sectionsObjects; } public Filter Filter { get; private set; } public override void NotifyDataSetChanged() { SetupIndex(); base.NotifyDataSetChanged(); } private class ChemicalFilter : Filter { private readonly ChemicalsAdapter _adapter; public ChemicalFilter(ChemicalsAdapter adapter) { _adapter = adapter; } protected override FilterResults PerformFiltering(ICharSequence constraint) { var returnObj = new FilterResults(); var results = new List<Chemical>(); if (_adapter._originalData == null) _adapter._originalData = _adapter._items; if (constraint == null) return returnObj; if (_adapter._originalData != null && _adapter._originalData.Any()) { results.AddRange( _adapter._originalData.Where( chemical => chemical.Name.ToLower().Contains(constraint.ToString()))); } returnObj.Values = FromArray(results.ToArray()); return returnObj; } protected override void PublishResults(ICharSequence constraint, FilterResults results) { var res = new List<Chemical>(results.Values.ToArray<Chemical>()); _adapter._items = res; _adapter.NotifyDataSetChanged(); } } } } 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,67 @@ using System.Collections.Generic; using Android.App; using Android.Runtime; using Android.Support.V4.View; using Android.Support.V7.App; using Android.Support.V7.Widget; using Android.Views; using Android.Widget; using Android.OS; namespace SearchViewSample { [Activity(Label = "SearchViewSample", MainLauncher = true, Icon = "@drawable/icon", Theme = "@style/Theme.AppCompat.Light")] public class Activity1 : ActionBarActivity { private SearchView _searchView; private ListView _listView; private ChemicalsAdapter _adapter; protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); // Set our view from the "main" layout resource SetContentView(Resource.Layout.Main); var chemicals = new List<Chemical> { new Chemical {Name = "Niacin"}, new Chemical {Name = "Biotin"}, new Chemical {Name = "Chromichlorid"}, new Chemical {Name = "Natriumselenit"}, new Chemical {Name = "Manganosulfate"}, new Chemical {Name = "Natriummolybdate"}, new Chemical {Name = "Ergocalciferol"}, new Chemical {Name = "Cyanocobalamin"}, }; _listView = FindViewById<ListView>(Resource.Id.listView); _adapter = new ChemicalsAdapter(this, chemicals); _listView.Adapter = _adapter; } public override bool OnCreateOptionsMenu(IMenu menu) { MenuInflater.Inflate(Resource.Menu.main, menu); var item = menu.FindItem(Resource.Id.action_search); var searchView = MenuItemCompat.GetActionView(item); _searchView = searchView.JavaCast<SearchView>(); _searchView.QueryTextChange += (s, e) => _adapter.Filter.InvokeFilter(e.NewText); _searchView.QueryTextSubmit += (s, e) => { Toast.MakeText(this, "Searched for: " + e.Query, ToastLength.Short).Show(); e.Handled = true; }; return true; } } }