-
Notifications
You must be signed in to change notification settings - Fork 0
/
MutliSelectionSpinner.cs
185 lines (168 loc) · 5.56 KB
/
MutliSelectionSpinner.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.Views;
using Android.Util;
using Android.Widget;
namespace androidui
{
class MultiSelectionSpinner : Spinner, IDialogInterfaceOnMultiChoiceClickListener
{
private string[] items;
private bool[] selection;
private ArrayAdapter<string> simpleAdapter;
private Context context;
public MultiSelectionSpinner(IntPtr a, Android.Runtime.JniHandleOwnership b) : base(a, b)
{
}
public MultiSelectionSpinner(Context c) : base(c, null)
{
context = c;
simpleAdapter = new ArrayAdapter<string>(context, Android.Resource.Layout.SimpleSpinnerItem);
Adapter = simpleAdapter;
}
public MultiSelectionSpinner(Context c, IAttributeSet attrs) : base(c, attrs)
{
context = c;
simpleAdapter = new ArrayAdapter<string>(context, Android.Resource.Layout.SimpleSpinnerItem);
Adapter = simpleAdapter;
}
public MultiSelectionSpinner(Context c, IAttributeSet attr, int defStyle) :
base(c, attr, defStyle)
{
context = c;
simpleAdapter = new ArrayAdapter<string>(context, Android.Resource.Layout.SimpleSpinnerItem);
Adapter = simpleAdapter;
}
public void OnClick(IDialogInterface dialog, int which, bool isChecked)
{
if (selection != null && which < selection.Length)
{
selection[which] = isChecked;
simpleAdapter.Clear();
simpleAdapter.Add(buildSelectedItemString());
}
else
{
throw new Exception("Argument 'which' is out of bounds");
}
}
public override bool PerformClick()
{
var builder = new AlertDialog.Builder(context);
builder.SetMultiChoiceItems(items, selection, this);
builder.Show();
return true;
}
public override ISpinnerAdapter Adapter
{
get
{
return base.Adapter;
}
set
{
throw new Exception("setAdapter is not supported by MultiSelectSpinner");
}
}
public void SetItems(string[] i)
{
items = i;
selection = new bool[items.Length];
simpleAdapter.Clear();
simpleAdapter.Add(items[0]);
selection = selection.Select(t => false).ToArray();
}
public void SetItems(List<string> i)
{
items = i.ToArray();
selection = new bool[items.Length];
simpleAdapter.Clear();
simpleAdapter.Add(items[0]);
selection = selection.Select(t => false).ToArray();
}
public void SetSelection(string[] sel)
{
foreach (var c in sel)
{
for (int j = 0; j < items.Length; ++j)
if (items[j] == c)
selection[j] = true;
}
}
public void SetSelection(List<string> sel)
{
selection = selection.Select(t => false).ToArray();
foreach (var s in sel)
{
for (int j = 0; j < items.Length; ++j)
if (items[j] == s)
selection[j] = true;
}
simpleAdapter.Clear();
simpleAdapter.Add(buildSelectedItemString());
}
public void SetSelection(int[] selectedIndices)
{
selection = selection.Select(t => false).ToArray();
foreach (var index in selectedIndices)
{
if (index >= 0 && index < selection.Length)
selection[index] = true;
else
{
throw new Exception(string.Format("Index {0} is out of bounds", index));
}
}
simpleAdapter.Clear();
simpleAdapter.Add(buildSelectedItemString());
}
public void SetSelection(int index)
{
selection = selection.Select(t => false).ToArray();
if (index >= 0 && index < selection.Length)
selection[index] = true;
else
{
throw new Exception(string.Format("Index {0} is out of bounds", index));
}
simpleAdapter.Clear();
simpleAdapter.Add(buildSelectedItemString());
}
public List<string> GetSelectedString()
{
var select = new List<string>();
for (int i = 0; i < items.Length; ++i)
if (selection[i])
select.Add(items[i]);
return select;
}
public List<int> GetSelectedIndices()
{
var select = new List<int>();
for (int i = 0; i < items.Length; ++i)
if (selection[i])
select.Add(i);
return select;
}
private string buildSelectedItemString()
{
var sb = new StringBuilder();
bool foundOne = false;
for (int i = 0; i < items.Length; ++i)
{
if (selection[i])
{
if (foundOne)
sb.Append(", ");
foundOne = true;
sb.Append(items[i]);
}
}
return sb.ToString();
}
}
}