質問編集履歴
3
コードの追加、変数の説明追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -2,7 +2,13 @@
|
|
2
2
|
|
3
3
|
```SelectActivity
|
4
4
|
|
5
|
+
public class SelectActivity extends AppCompatActivity {
|
6
|
+
|
7
|
+
private static ArrayList<Card> targetList;
|
8
|
+
|
9
|
+
|
10
|
+
|
5
|
-
@Override
|
11
|
+
@Override
|
6
12
|
|
7
13
|
protected void onCreate(Bundle savedInstanceState) {
|
8
14
|
|
@@ -12,7 +18,23 @@
|
|
12
18
|
|
13
19
|
|
14
20
|
|
21
|
+
super.onCreate(savedInstanceState);
|
22
|
+
|
23
|
+
setContentView(R.layout.activity_select);
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
Toolbar toolbar = findViewById(R.id.tool_bar);
|
28
|
+
|
15
|
-
|
29
|
+
setSupportActionBar(toolbar);
|
30
|
+
|
31
|
+
toolbar.setTitle(R.string.choose_cards);
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
|
36
|
+
|
37
|
+
getSupportActionBar().setHomeButtonEnabled(true);
|
16
38
|
|
17
39
|
|
18
40
|
|
@@ -82,7 +104,7 @@
|
|
82
104
|
|
83
105
|
MyArrayAdapter adapter = new MyArrayAdapter(
|
84
106
|
|
85
|
-
this, android.R.layout.simple_list_item_multiple_choice, new ArrayList<String>(Arrays.asList(MainActivity.cardNames)), targetList
|
107
|
+
this, android.R.layout.simple_list_item_multiple_choice, new ArrayList<String>(Arrays.asList(MainActivity.cardNames)), targetList);
|
86
108
|
|
87
109
|
|
88
110
|
|
@@ -134,13 +156,53 @@
|
|
134
156
|
|
135
157
|
}
|
136
158
|
|
159
|
+
}
|
160
|
+
|
137
161
|
```
|
138
162
|
|
139
163
|
|
140
164
|
|
141
165
|
```MyArrayAdapter
|
142
166
|
|
167
|
+
public class MyArrayAdapter extends ArrayAdapter {
|
168
|
+
|
169
|
+
|
170
|
+
|
171
|
+
private ArrayList<String> cardNames;
|
172
|
+
|
173
|
+
private ArrayList<Card> targetList;
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
private TestFilter _filter;
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
public MyArrayAdapter(Context context, int resourceId, ArrayList<String> cardNames, ArrayList<Card> targetList){
|
182
|
+
|
183
|
+
super(context, resourceId, cardNames);
|
184
|
+
|
185
|
+
this.cardNames = (ArrayList<String>)cardNames.clone();
|
186
|
+
|
187
|
+
this.targetList = targetList;
|
188
|
+
|
189
|
+
this.otherList = otherList;
|
190
|
+
|
191
|
+
}
|
192
|
+
|
193
|
+
|
194
|
+
|
143
|
-
@Override
|
195
|
+
@Override
|
196
|
+
|
197
|
+
public boolean areAllItemsEnabled(){
|
198
|
+
|
199
|
+
return true;
|
200
|
+
|
201
|
+
}
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
@Override
|
144
206
|
|
145
207
|
public View getView(int position, View convertView, ViewGroup parent){
|
146
208
|
|
@@ -256,4 +318,12 @@
|
|
256
318
|
|
257
319
|
}
|
258
320
|
|
321
|
+
}
|
322
|
+
|
259
323
|
```
|
324
|
+
|
325
|
+
|
326
|
+
|
327
|
+
追記
|
328
|
+
|
329
|
+
MainActivityにあるcardList(Arraylist<Card>)は、idとnameを持つCardクラスのオブジェクトの集合で、このnameのみのstring配列がcardNamesです。selectActivityから離れるときにチェックを入れていたところのCardオブジェクトの配列をtargetList(Arraylist<Card>)に入れようとしています。
|
2
タイトル更新
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
チェックボックス付ListviewでSearchviewによるフィルターがかからない
|
1
|
+
[Android Studio]チェックボックス付ListviewでSearchviewによるフィルターがかからない
|
test
CHANGED
File without changes
|
1
MyArrayAdaterのコード追記など
test
CHANGED
File without changes
|
test
CHANGED
@@ -54,7 +54,31 @@
|
|
54
54
|
|
55
55
|
});
|
56
56
|
|
57
|
+
|
58
|
+
|
59
|
+
@Override
|
60
|
+
|
61
|
+
public void onResume(){
|
62
|
+
|
63
|
+
super.onResume();
|
64
|
+
|
65
|
+
checkUpdate();
|
66
|
+
|
67
|
+
}
|
68
|
+
|
69
|
+
@Override
|
70
|
+
|
71
|
+
public void onPause(){
|
72
|
+
|
73
|
+
super.onPause();
|
74
|
+
|
75
|
+
resisterTargetList();
|
76
|
+
|
77
|
+
}
|
78
|
+
|
79
|
+
|
80
|
+
|
57
|
-
private void setList(){
|
81
|
+
private void setList(){
|
58
82
|
|
59
83
|
MyArrayAdapter adapter = new MyArrayAdapter(
|
60
84
|
|
@@ -82,8 +106,154 @@
|
|
82
106
|
|
83
107
|
listView.setItemChecked(id, true);
|
84
108
|
|
85
|
-
Log.d("yeah", card.getName() + "にチェック");
|
86
|
-
|
87
|
-
}
|
109
|
+
}
|
110
|
+
|
111
|
+
}
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
public static void resisterTargetList(){
|
116
|
+
|
117
|
+
SparseBooleanArray array = listView.getCheckedItemPositions();
|
118
|
+
|
119
|
+
targetList.clear();
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
for(int i = 0; i < array.size(); i++){
|
124
|
+
|
125
|
+
int at = array.keyAt(i);
|
126
|
+
|
127
|
+
if(array.get(at)){
|
128
|
+
|
129
|
+
targetList.add(MainActivity.cardList.get(at));
|
130
|
+
|
131
|
+
}
|
132
|
+
|
133
|
+
}
|
134
|
+
|
135
|
+
}
|
88
136
|
|
89
137
|
```
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
```MyArrayAdapter
|
142
|
+
|
143
|
+
@Override
|
144
|
+
|
145
|
+
public View getView(int position, View convertView, ViewGroup parent){
|
146
|
+
|
147
|
+
View view = super.getView(position, convertView, parent);
|
148
|
+
|
149
|
+
return view;
|
150
|
+
|
151
|
+
}
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
@Override
|
156
|
+
|
157
|
+
public Filter getFilter() {
|
158
|
+
|
159
|
+
if(_filter == null) {
|
160
|
+
|
161
|
+
_filter = new TestFilter();
|
162
|
+
|
163
|
+
}
|
164
|
+
|
165
|
+
return _filter;
|
166
|
+
|
167
|
+
}
|
168
|
+
|
169
|
+
|
170
|
+
|
171
|
+
public class TestFilter extends Filter {
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
@Override
|
176
|
+
|
177
|
+
protected FilterResults performFiltering(CharSequence constraint) {
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
SelectActivity.resisterTargetList();
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
ArrayList<String> items = new ArrayList<>();
|
186
|
+
|
187
|
+
if (constraint == null || constraint.length() == 0) {
|
188
|
+
|
189
|
+
items = new ArrayList<String>(cardNames);
|
190
|
+
|
191
|
+
} else {
|
192
|
+
|
193
|
+
// getCount及びgetItemはAdapterのメソッド
|
194
|
+
|
195
|
+
for(int i = 0, size = cardNames.size(); i < size; i++) {
|
196
|
+
|
197
|
+
String data = cardNames.get(i);
|
198
|
+
|
199
|
+
if(data.contains(constraint)) {
|
200
|
+
|
201
|
+
items.add(data);
|
202
|
+
|
203
|
+
}
|
204
|
+
|
205
|
+
}
|
206
|
+
|
207
|
+
}
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
FilterResults r = new FilterResults();
|
212
|
+
|
213
|
+
r.count = items.size();
|
214
|
+
|
215
|
+
r.values = items;
|
216
|
+
|
217
|
+
SelectActivity.checkUpdate();
|
218
|
+
|
219
|
+
return r;
|
220
|
+
|
221
|
+
}
|
222
|
+
|
223
|
+
|
224
|
+
|
225
|
+
@SuppressWarnings("unchecked")
|
226
|
+
|
227
|
+
@Override
|
228
|
+
|
229
|
+
protected void publishResults(CharSequence constraint, Filter.FilterResults results) {
|
230
|
+
|
231
|
+
|
232
|
+
|
233
|
+
// Adapterのメソッドでデータの内容を更新する
|
234
|
+
|
235
|
+
if(results != null && results.count > 0) {
|
236
|
+
|
237
|
+
|
238
|
+
|
239
|
+
List<String> items = (List<String>) results.values;
|
240
|
+
|
241
|
+
clear();
|
242
|
+
|
243
|
+
addAll(items);
|
244
|
+
|
245
|
+
|
246
|
+
|
247
|
+
notifyDataSetChanged();
|
248
|
+
|
249
|
+
} else {
|
250
|
+
|
251
|
+
notifyDataSetInvalidated();
|
252
|
+
|
253
|
+
}
|
254
|
+
|
255
|
+
}
|
256
|
+
|
257
|
+
}
|
258
|
+
|
259
|
+
```
|