質問編集履歴
4
修正コード記載
test
CHANGED
File without changes
|
test
CHANGED
@@ -369,3 +369,105 @@
|
|
369
369
|
|
370
370
|
|
371
371
|
テストコードを追記しました。
|
372
|
+
|
373
|
+
|
374
|
+
|
375
|
+
|
376
|
+
|
377
|
+
|
378
|
+
|
379
|
+
|
380
|
+
|
381
|
+
==修正コード==
|
382
|
+
|
383
|
+
|
384
|
+
|
385
|
+
Fragment Class側
|
386
|
+
|
387
|
+
```java
|
388
|
+
|
389
|
+
public void onViewCreated(View view, Bundle savedInstanceState){
|
390
|
+
|
391
|
+
super.onViewCreated(view, savedInstanceState);
|
392
|
+
|
393
|
+
|
394
|
+
|
395
|
+
// View初期化
|
396
|
+
|
397
|
+
mRoot = (LinearLayout)view.findViewById(R.id.root);
|
398
|
+
|
399
|
+
|
400
|
+
|
401
|
+
listView = (ListView) view.findViewById(R.id.list_collection);
|
402
|
+
|
403
|
+
listView.setOnItemClickListener(this);
|
404
|
+
|
405
|
+
listView.setOnCreateContextMenuListener(this);
|
406
|
+
|
407
|
+
ListView listView = (ListView)getView().findViewById(R.id.list_collection);
|
408
|
+
|
409
|
+
CollectionListAdapter adapter = (CollectionListAdapter)listView.getAdapter();
|
410
|
+
|
411
|
+
|
412
|
+
|
413
|
+
Bundle bundle = getArguments();
|
414
|
+
|
415
|
+
collectionList = testView(bundle.getInt("position"));
|
416
|
+
|
417
|
+
if(adapter == null){
|
418
|
+
|
419
|
+
adapter = new CollectionListAdapter(getActivity(), collectionList);
|
420
|
+
|
421
|
+
listView.setAdapter(adapter);
|
422
|
+
|
423
|
+
}
|
424
|
+
|
425
|
+
adapter.updateCollectionList(collectionList);
|
426
|
+
|
427
|
+
}
|
428
|
+
|
429
|
+
```
|
430
|
+
|
431
|
+
|
432
|
+
|
433
|
+
|
434
|
+
|
435
|
+
呼び出し元
|
436
|
+
|
437
|
+
```java
|
438
|
+
|
439
|
+
@Override
|
440
|
+
|
441
|
+
public Fragment getItem(int position) {
|
442
|
+
|
443
|
+
|
444
|
+
|
445
|
+
Bundle bundle = new Bundle();;
|
446
|
+
|
447
|
+
|
448
|
+
|
449
|
+
bundle.putInt("position",position);
|
450
|
+
|
451
|
+
|
452
|
+
|
453
|
+
CountryFragment fragment = new CountryFragment();
|
454
|
+
|
455
|
+
fragment.setArguments(bundle);
|
456
|
+
|
457
|
+
|
458
|
+
|
459
|
+
getFragmentManager().beginTransaction().add(R.id.container,fragment,"TAG").commit();
|
460
|
+
|
461
|
+
|
462
|
+
|
463
|
+
return fragment;
|
464
|
+
|
465
|
+
}
|
466
|
+
|
467
|
+
```
|
468
|
+
|
469
|
+
|
470
|
+
|
471
|
+
これだと、 getFragmentManager().beginTransaction().add(R.id.container,fragment,"TAG").commit();のfragmentにエラーメッセージが出ていて、コンパイルが通りません。
|
472
|
+
|
473
|
+
エラーメッセージは「Wrong 2nd argument type. Found: 'testproject.fragment.CountryFragment', required: 'android.app.Fragment」です。
|
3
呼び出し元コード追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -348,4 +348,24 @@
|
|
348
348
|
|
349
349
|
|
350
350
|
|
351
|
+
呼び出し元
|
352
|
+
|
353
|
+
```java
|
354
|
+
|
355
|
+
@Override
|
356
|
+
|
357
|
+
public Fragment getItem(int position) {
|
358
|
+
|
359
|
+
return CountryFragment.newInstance(position);
|
360
|
+
|
361
|
+
}
|
362
|
+
|
363
|
+
```
|
364
|
+
|
365
|
+
|
366
|
+
|
367
|
+
ここのpositionにきている値がおかしい。
|
368
|
+
|
369
|
+
|
370
|
+
|
351
371
|
テストコードを追記しました。
|
2
テストコード追記しました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -10,6 +10,18 @@
|
|
10
10
|
|
11
11
|
|
12
12
|
|
13
|
+
|
14
|
+
|
15
|
+
|
16
|
+
|
17
|
+
ちなみに、onResumeに書くと、起動時にエクセプションでアプリが落ちてしまいます。
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
よろしくお願いします。
|
22
|
+
|
23
|
+
|
24
|
+
|
13
25
|
```java
|
14
26
|
|
15
27
|
private void updateData(final Activity activity){
|
@@ -56,8 +68,284 @@
|
|
56
68
|
|
57
69
|
|
58
70
|
|
71
|
+
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
テストクラスを追記↓
|
76
|
+
|
77
|
+
```java
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
public class CountryFragment extends Fragment implements View.OnClickListener,AdapterView.OnItemClickListener{
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
// シングルトンで生成
|
88
|
+
|
89
|
+
private static CountryFragment mCountryFragment;
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
private Context mContext;
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
private LinearLayout mRoot;
|
98
|
+
|
99
|
+
private ListView listView;
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
private static int number;
|
104
|
+
|
105
|
+
private static Tab tab;
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
private CollectionListAdapter mCollectionListAdapter;
|
110
|
+
|
111
|
+
private List<Collection> collectionList;
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
public static CountryFragment newInstance(int position){
|
118
|
+
|
119
|
+
mCountryFragment = new CountryFragment();
|
120
|
+
|
121
|
+
number = position;
|
122
|
+
|
123
|
+
return mCountryFragment;
|
124
|
+
|
125
|
+
}
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
public static CountryFragment getInstance(){
|
130
|
+
|
131
|
+
return mCountryFragment;
|
132
|
+
|
133
|
+
}
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
// 空のコンストラクタ必要
|
138
|
+
|
139
|
+
public CountryFragment() {}
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
@Override
|
144
|
+
|
145
|
+
public void onAttach(Context context){
|
146
|
+
|
147
|
+
super.onAttach(context);
|
148
|
+
|
149
|
+
mContext = context;
|
150
|
+
|
151
|
+
}
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
// ビューを生成
|
156
|
+
|
157
|
+
@Override
|
158
|
+
|
159
|
+
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceSate){
|
160
|
+
|
161
|
+
return inflater.inflate(R.layout.fragment_all,container,false);
|
162
|
+
|
163
|
+
}
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
@Override
|
168
|
+
|
169
|
+
public void onResume(){
|
170
|
+
|
171
|
+
super.onResume();
|
172
|
+
|
173
|
+
}
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
// こっちもビューを生成
|
180
|
+
|
181
|
+
@Override
|
182
|
+
|
183
|
+
public void onViewCreated(View view, Bundle savedInstanceState){
|
184
|
+
|
59
|
-
|
185
|
+
super.onViewCreated(view, savedInstanceState);
|
186
|
+
|
187
|
+
|
188
|
+
|
60
|
-
|
189
|
+
// View初期化
|
190
|
+
|
61
|
-
|
191
|
+
mRoot = (LinearLayout)view.findViewById(R.id.root);
|
192
|
+
|
193
|
+
|
194
|
+
|
62
|
-
|
195
|
+
listView = (ListView) view.findViewById(R.id.list_collection);
|
196
|
+
|
197
|
+
listView.setOnItemClickListener(this);
|
198
|
+
|
199
|
+
listView.setOnCreateContextMenuListener(this);
|
200
|
+
|
201
|
+
ListView listView = (ListView)getView().findViewById(R.id.list_collection);
|
202
|
+
|
203
|
+
CollectionListAdapter adapter = (CollectionListAdapter)listView.getAdapter();
|
204
|
+
|
205
|
+
collectionList = testView(number);
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
if(adapter == null){
|
210
|
+
|
211
|
+
adapter = new CollectionListAdapter(getActivity(), collectionList);
|
212
|
+
|
213
|
+
listView.setAdapter(adapter);
|
214
|
+
|
215
|
+
}
|
216
|
+
|
217
|
+
adapter.updateCollectionList(collectionList);
|
218
|
+
|
219
|
+
}
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
public List<Collection> testView(int number){
|
224
|
+
|
225
|
+
List<Collection> collectionList = new ArrayList<>();
|
226
|
+
|
227
|
+
|
228
|
+
|
229
|
+
if(number == 0){
|
230
|
+
|
231
|
+
Collection collection1 = new Collection(0);
|
232
|
+
|
233
|
+
collection1.setName("テスト1");
|
234
|
+
|
235
|
+
collection1.setType1(1);
|
236
|
+
|
237
|
+
collection1.setAmazon("http://xxxxxx.com");
|
238
|
+
|
239
|
+
collectionList.add(collection1);
|
240
|
+
|
241
|
+
}else if(number ==1){
|
242
|
+
|
243
|
+
Collection collection1 = new Collection(0);
|
244
|
+
|
245
|
+
collection1.setName("テスト1");
|
246
|
+
|
247
|
+
collection1.setType1(1);
|
248
|
+
|
249
|
+
collection1.setAmazon("http://xxxxxx.com");
|
250
|
+
|
251
|
+
collectionList.add(collection1);
|
252
|
+
|
253
|
+
Collection collection2 = new Collection(0);
|
254
|
+
|
255
|
+
collection2.setName("テスト2");
|
256
|
+
|
257
|
+
collection2.setType1(2);
|
258
|
+
|
259
|
+
collection2.setAmazon("http://xxxxxx2.com");
|
260
|
+
|
261
|
+
collectionList.add(collection1);
|
262
|
+
|
263
|
+
}else if(number==2){
|
264
|
+
|
265
|
+
Collection collection1 = new Collection(0);
|
266
|
+
|
267
|
+
collection1.setName("テスト1");
|
268
|
+
|
269
|
+
collection1.setType1(1);
|
270
|
+
|
271
|
+
collection1.setAmazon("http://xxxxxx.com");
|
272
|
+
|
273
|
+
collectionList.add(collection1);
|
274
|
+
|
275
|
+
Collection collection2 = new Collection(0);
|
276
|
+
|
277
|
+
collection2.setName("テスト1");
|
278
|
+
|
279
|
+
collection2.setType1(2);
|
280
|
+
|
281
|
+
collection2.setAmazon("http://xxxxxx.com");
|
282
|
+
|
283
|
+
collectionList.add(collection2);
|
284
|
+
|
285
|
+
Collection collection3 = new Collection(0);
|
286
|
+
|
287
|
+
collection3.setName("テスト1");
|
288
|
+
|
289
|
+
collection3.setType1(3);
|
290
|
+
|
291
|
+
collection3.setAmazon("http://xxxxxx.com");
|
292
|
+
|
293
|
+
collectionList.add(collection3);
|
294
|
+
|
295
|
+
}else{
|
296
|
+
|
297
|
+
Collection collection1 = new Collection(0);
|
298
|
+
|
299
|
+
collection1.setName("テスト1");
|
300
|
+
|
301
|
+
collection1.setType1(1);
|
302
|
+
|
303
|
+
collection1.setAmazon("http://xxxxxx.com");
|
304
|
+
|
305
|
+
collectionList.add(collection1);
|
306
|
+
|
307
|
+
Collection collection2 = new Collection(0);
|
308
|
+
|
309
|
+
collection2.setName("テスト1");
|
310
|
+
|
311
|
+
collection2.setType1(2);
|
312
|
+
|
313
|
+
collection2.setAmazon("http://xxxxxx.com");
|
314
|
+
|
315
|
+
collectionList.add(collection2);
|
316
|
+
|
317
|
+
Collection collection3 = new Collection(0);
|
318
|
+
|
319
|
+
collection3.setName("テスト1");
|
320
|
+
|
321
|
+
collection3.setType1(3);
|
322
|
+
|
323
|
+
collection3.setAmazon("http://xxxxxx.com");
|
324
|
+
|
325
|
+
collectionList.add(collection3);
|
326
|
+
|
327
|
+
Collection collection4 = new Collection(0);
|
328
|
+
|
329
|
+
collection4.setName("テスト1");
|
330
|
+
|
331
|
+
collection4.setType1(4);
|
332
|
+
|
333
|
+
collection4.setAmazon("http://xxxxxx.com");
|
334
|
+
|
335
|
+
collectionList.add(collection4);
|
336
|
+
|
337
|
+
}
|
338
|
+
|
339
|
+
|
340
|
+
|
341
|
+
return collectionList;
|
342
|
+
|
343
|
+
|
344
|
+
|
345
|
+
}
|
346
|
+
|
347
|
+
```
|
348
|
+
|
349
|
+
|
350
|
+
|
63
|
-
|
351
|
+
テストコードを追記しました。
|
1
updateDataコードを追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -10,6 +10,52 @@
|
|
10
10
|
|
11
11
|
|
12
12
|
|
13
|
+
```java
|
14
|
+
|
15
|
+
private void updateData(final Activity activity){
|
16
|
+
|
17
|
+
new Thread(new Runnable() {
|
18
|
+
|
19
|
+
@Override
|
20
|
+
|
21
|
+
public void run() {
|
22
|
+
|
23
|
+
collectionList = ((infoCollector) getActivity().getApplication()).getinfoCollectorDatabase().getCollectionList(tab);
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
activity.runOnUiThread(new Runnable() {
|
28
|
+
|
29
|
+
@Override
|
30
|
+
|
31
|
+
public void run() {
|
32
|
+
|
33
|
+
CollectionListAdapter adapter = (CollectionListAdapter)listView.getAdapter();
|
34
|
+
|
35
|
+
if(adapter == null){
|
36
|
+
|
37
|
+
adapter = new CollectionListAdapter(activity, collectionList);
|
38
|
+
|
39
|
+
listView.setAdapter(adapter);
|
40
|
+
|
41
|
+
}
|
42
|
+
|
43
|
+
adapter.updateCollectionList(collectionList);
|
44
|
+
|
45
|
+
}
|
46
|
+
|
47
|
+
});
|
48
|
+
|
49
|
+
}
|
50
|
+
|
51
|
+
}).start();
|
52
|
+
|
53
|
+
}
|
54
|
+
|
55
|
+
```
|
56
|
+
|
57
|
+
|
58
|
+
|
13
59
|
ちなみに、onResumeに書くと、起動時にエクセプションでアプリが落ちてしまいます。
|
14
60
|
|
15
61
|
|