/
MainActivity.cs
161 lines (134 loc) · 4.31 KB
/
MainActivity.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using System.Collections.Generic;
using System.Linq;
namespace ListViewFilter
{
[Activity (Label = "ListViewFilter", MainLauncher = true)]
public class MainActivity : Activity
{
Button _button;
ListView _listview;
EditText _filterText;
FilterableAdapter _adapter;
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);
_button = FindViewById<Button> (Resource.Id.dosearch);
_listview = FindViewById<ListView> (Resource.Id.list);
_filterText = FindViewById<EditText> (Resource.Id.search);
_adapter = new FilterableAdapter (this, Android.Resource.Layout.SimpleListItem1, GetItems());
_listview.Adapter = _adapter;
_button.Click += delegate {
// filter the adapter here
_adapter.Filter.InvokeFilter(_filterText.Text);
};
_filterText.TextChanged += (object sender, Android.Text.TextChangedEventArgs e) => {
// filter on text changed
var searchTerm = _filterText.Text;
if (String.IsNullOrEmpty(searchTerm)) {
_adapter.ResetSearch();
} else {
_adapter.Filter.InvokeFilter(searchTerm);
}
};
}
string[] GetItems ()
{
string[] names = new string[] { "Ljubljana 280,000", "Maribor 130,000", "Celje 56,000", "Kranj 52,000", "Novo Mesto 48,000", "Nova Gorica 45,000", "Koper/Capodistria 42,000", "Velenje 32,000", "Domžale 30,000", "Ptuj 25,000", "Murska Sobota 22,000", "Jesenice 20,000", "Trbovlje 16,000", "Kamnik 15,000", "Škofja Loka 12,000", "Izola/Isola 11,000" };
Random r = new Random ();
List<string> res = new List<string> ();
for (int i = 0; i < 300; i++) {
res.Add (names [r.Next (names.Length)]);
}
return res.ToArray ();
}
}
public class FilterableAdapter : ArrayAdapter, IFilterable
{
LayoutInflater inflater;
Filter filter;
Activity context;
public string[] AllItems;
public string[] MatchItems;
public FilterableAdapter (Activity context, int txtViewResourceId, string[] items) : base(context, txtViewResourceId, items)
{
inflater = context.LayoutInflater;
filter = new SuggestionsFilter(this);
AllItems = items;
MatchItems = items;
}
public override int Count {
get {
return MatchItems.Length;
}
}
public override Java.Lang.Object GetItem (int position)
{
return MatchItems[position];
}
public override View GetView (int position, View convertView, ViewGroup parent)
{
View view = convertView;
if (view == null)
view = inflater.Inflate(Android.Resource.Layout.SimpleDropDownItem1Line, null);
view.FindViewById<TextView>(Android.Resource.Id.Text1).Text = MatchItems[position];
return view;
}
public override Filter Filter {
get {
return filter;
}
}
public void ResetSearch() {
MatchItems = AllItems;
NotifyDataSetChanged ();
}
class SuggestionsFilter : Filter
{
readonly FilterableAdapter _adapter;
public SuggestionsFilter (FilterableAdapter adapter) : base() {
_adapter = adapter;
}
protected override Filter.FilterResults PerformFiltering (Java.Lang.ICharSequence constraint)
{
FilterResults results = new FilterResults();
if (!String.IsNullOrEmpty (constraint.ToString ())) {
var searchFor = constraint.ToString ();
Console.WriteLine ("searchFor:" + searchFor);
var matchList = new List<string> ();
var matches =
from i in _adapter.AllItems
where i.IndexOf (searchFor, StringComparison.InvariantCultureIgnoreCase) >= 0
select i;
foreach (var match in matches) {
matchList.Add (match);
}
_adapter.MatchItems = matchList.ToArray ();
Console.WriteLine ("resultCount:" + matchList.Count);
Java.Lang.Object[] matchObjects;
matchObjects = new Java.Lang.Object[matchList.Count];
for (int i = 0; i < matchList.Count; i++) {
matchObjects [i] = new Java.Lang.String (matchList [i]);
}
results.Values = matchObjects;
results.Count = matchList.Count;
} else {
_adapter.ResetSearch ();
}
return results;
}
protected override void PublishResults (Java.Lang.ICharSequence constraint, Filter.FilterResults results)
{
_adapter.NotifyDataSetChanged();
}
}
}
}