質問編集履歴
3
コードの追加、変数の説明追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,12 +1,23 @@
|
|
1
1
|
タイトルの通りです。listviewに並んだ要素それぞれにチェックボックス(複数選択可)があり、チェックを入れたものの配列を保存する、というものに、searchviewによる検索機能を足そうとしてみました。(検索を前方一致以外にするにはadapterのgetFilterとかをいじる、と見たので、そうしました。)その結果、チェックを一つも打っていない状態なら検索が正常に動作するのに、1つでもチェックが入っている状態からは検索ワードを打ち込んでも反応しない、という状況になってしまいました。
|
2
2
|
```SelectActivity
|
3
|
+
public class SelectActivity extends AppCompatActivity {
|
4
|
+
private static ArrayList<Card> targetList;
|
5
|
+
|
3
|
-
@Override
|
6
|
+
@Override
|
4
7
|
protected void onCreate(Bundle savedInstanceState) {
|
5
8
|
super.onCreate(savedInstanceState);
|
6
9
|
setContentView(R.layout.activity_select);
|
7
10
|
|
8
|
-
|
11
|
+
super.onCreate(savedInstanceState);
|
12
|
+
setContentView(R.layout.activity_select);
|
9
13
|
|
14
|
+
Toolbar toolbar = findViewById(R.id.tool_bar);
|
15
|
+
setSupportActionBar(toolbar);
|
16
|
+
toolbar.setTitle(R.string.choose_cards);
|
17
|
+
|
18
|
+
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
|
19
|
+
getSupportActionBar().setHomeButtonEnabled(true);
|
20
|
+
|
10
21
|
setList();
|
11
22
|
|
12
23
|
searchView = findViewById(R.id.searchView);
|
@@ -40,7 +51,7 @@
|
|
40
51
|
|
41
52
|
private void setList(){
|
42
53
|
MyArrayAdapter adapter = new MyArrayAdapter(
|
43
|
-
this, android.R.layout.simple_list_item_multiple_choice, new ArrayList<String>(Arrays.asList(MainActivity.cardNames)), targetList
|
54
|
+
this, android.R.layout.simple_list_item_multiple_choice, new ArrayList<String>(Arrays.asList(MainActivity.cardNames)), targetList);
|
44
55
|
|
45
56
|
listView = findViewById(R.id.listview);
|
46
57
|
listView.setAdapter(adapter);
|
@@ -66,10 +77,30 @@
|
|
66
77
|
}
|
67
78
|
}
|
68
79
|
}
|
80
|
+
}
|
69
81
|
```
|
70
82
|
|
71
83
|
```MyArrayAdapter
|
84
|
+
public class MyArrayAdapter extends ArrayAdapter {
|
85
|
+
|
86
|
+
private ArrayList<String> cardNames;
|
87
|
+
private ArrayList<Card> targetList;
|
88
|
+
|
89
|
+
private TestFilter _filter;
|
90
|
+
|
91
|
+
public MyArrayAdapter(Context context, int resourceId, ArrayList<String> cardNames, ArrayList<Card> targetList){
|
92
|
+
super(context, resourceId, cardNames);
|
93
|
+
this.cardNames = (ArrayList<String>)cardNames.clone();
|
94
|
+
this.targetList = targetList;
|
95
|
+
this.otherList = otherList;
|
96
|
+
}
|
97
|
+
|
72
|
-
|
98
|
+
@Override
|
99
|
+
public boolean areAllItemsEnabled(){
|
100
|
+
return true;
|
101
|
+
}
|
102
|
+
|
103
|
+
@Override
|
73
104
|
public View getView(int position, View convertView, ViewGroup parent){
|
74
105
|
View view = super.getView(position, convertView, parent);
|
75
106
|
return view;
|
@@ -127,4 +158,8 @@
|
|
127
158
|
}
|
128
159
|
}
|
129
160
|
}
|
161
|
+
}
|
130
|
-
```
|
162
|
+
```
|
163
|
+
|
164
|
+
追記
|
165
|
+
MainActivityにあるcardList(Arraylist<Card>)は、idとnameを持つCardクラスのオブジェクトの集合で、このnameのみのstring配列がcardNamesです。selectActivityから離れるときにチェックを入れていたところのCardオブジェクトの配列をtargetList(Arraylist<Card>)に入れようとしています。
|
2
タイトル更新
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
チェックボックス付ListviewでSearchviewによるフィルターがかからない
|
1
|
+
[Android Studio]チェックボックス付ListviewでSearchviewによるフィルターがかからない
|
body
CHANGED
File without changes
|
1
MyArrayAdaterのコード追記など
title
CHANGED
File without changes
|
body
CHANGED
@@ -26,7 +26,19 @@
|
|
26
26
|
return true;
|
27
27
|
}
|
28
28
|
});
|
29
|
+
|
30
|
+
@Override
|
31
|
+
public void onResume(){
|
32
|
+
super.onResume();
|
33
|
+
checkUpdate();
|
34
|
+
}
|
35
|
+
@Override
|
36
|
+
public void onPause(){
|
37
|
+
super.onPause();
|
38
|
+
resisterTargetList();
|
39
|
+
}
|
40
|
+
|
29
|
-
private void setList(){
|
41
|
+
private void setList(){
|
30
42
|
MyArrayAdapter adapter = new MyArrayAdapter(
|
31
43
|
this, android.R.layout.simple_list_item_multiple_choice, new ArrayList<String>(Arrays.asList(MainActivity.cardNames)), targetList, otherList);
|
32
44
|
|
@@ -40,6 +52,79 @@
|
|
40
52
|
for(Card card: targetList){
|
41
53
|
int id = Arrays.asList(MainActivity.cardNames).indexOf(card.getName());
|
42
54
|
listView.setItemChecked(id, true);
|
43
|
-
Log.d("yeah", card.getName() + "にチェック");
|
44
55
|
}
|
56
|
+
}
|
57
|
+
|
58
|
+
public static void resisterTargetList(){
|
59
|
+
SparseBooleanArray array = listView.getCheckedItemPositions();
|
60
|
+
targetList.clear();
|
61
|
+
|
62
|
+
for(int i = 0; i < array.size(); i++){
|
63
|
+
int at = array.keyAt(i);
|
64
|
+
if(array.get(at)){
|
65
|
+
targetList.add(MainActivity.cardList.get(at));
|
66
|
+
}
|
67
|
+
}
|
68
|
+
}
|
69
|
+
```
|
70
|
+
|
71
|
+
```MyArrayAdapter
|
72
|
+
@Override
|
73
|
+
public View getView(int position, View convertView, ViewGroup parent){
|
74
|
+
View view = super.getView(position, convertView, parent);
|
75
|
+
return view;
|
76
|
+
}
|
77
|
+
|
78
|
+
@Override
|
79
|
+
public Filter getFilter() {
|
80
|
+
if(_filter == null) {
|
81
|
+
_filter = new TestFilter();
|
82
|
+
}
|
83
|
+
return _filter;
|
84
|
+
}
|
85
|
+
|
86
|
+
public class TestFilter extends Filter {
|
87
|
+
|
88
|
+
@Override
|
89
|
+
protected FilterResults performFiltering(CharSequence constraint) {
|
90
|
+
|
91
|
+
SelectActivity.resisterTargetList();
|
92
|
+
|
93
|
+
ArrayList<String> items = new ArrayList<>();
|
94
|
+
if (constraint == null || constraint.length() == 0) {
|
95
|
+
items = new ArrayList<String>(cardNames);
|
96
|
+
} else {
|
97
|
+
// getCount及びgetItemはAdapterのメソッド
|
98
|
+
for(int i = 0, size = cardNames.size(); i < size; i++) {
|
99
|
+
String data = cardNames.get(i);
|
100
|
+
if(data.contains(constraint)) {
|
101
|
+
items.add(data);
|
102
|
+
}
|
103
|
+
}
|
104
|
+
}
|
105
|
+
|
106
|
+
FilterResults r = new FilterResults();
|
107
|
+
r.count = items.size();
|
108
|
+
r.values = items;
|
109
|
+
SelectActivity.checkUpdate();
|
110
|
+
return r;
|
111
|
+
}
|
112
|
+
|
113
|
+
@SuppressWarnings("unchecked")
|
114
|
+
@Override
|
115
|
+
protected void publishResults(CharSequence constraint, Filter.FilterResults results) {
|
116
|
+
|
117
|
+
// Adapterのメソッドでデータの内容を更新する
|
118
|
+
if(results != null && results.count > 0) {
|
119
|
+
|
120
|
+
List<String> items = (List<String>) results.values;
|
121
|
+
clear();
|
122
|
+
addAll(items);
|
123
|
+
|
124
|
+
notifyDataSetChanged();
|
125
|
+
} else {
|
126
|
+
notifyDataSetInvalidated();
|
127
|
+
}
|
128
|
+
}
|
129
|
+
}
|
45
130
|
```
|