質問編集履歴

1

コードを追記

2016/10/21 06:16

投稿

intenseG
intenseG

スコア34

test CHANGED
File without changes
test CHANGED
@@ -46,13 +46,9 @@
46
46
 
47
47
  ```Java
48
48
 
49
- public class GoDataAdapter extends ArrayAdapter<GoData> {
49
+
50
-
51
-
52
-
50
+
53
- private LayoutInflater mLayoutInflater;
51
+ ------------省略----------------
54
-
55
- private ArrayList<GoData> mGoDataArrayList;
56
52
 
57
53
 
58
54
 
@@ -114,57 +110,157 @@
114
110
 
115
111
 
116
112
 
113
+ ###追記
114
+
115
+
116
+
117
117
  ProblemList.java
118
118
 
119
119
 
120
120
 
121
121
  ```Java
122
122
 
123
- public class ProblemList extends AppCompatActivity{
123
+
124
-
125
-
126
-
127
- public static final String EXTRA_GODATA = "com.example.oubeika.tsumegonomori.Problem";
124
+
128
-
129
-
130
-
131
- private ListView mListView;
125
+ ------------省略----------------
132
-
133
- private GoDataAdapter mGoDataAdapter;
126
+
134
-
135
- private Realm mRealm;
127
+
136
-
137
- private RealmResults<GoData> mGoDataRealmResults;
128
+
138
-
139
- private RealmChangeListener mRealmListener = new RealmChangeListener() {
140
-
141
-
142
-
143
- @Override
129
+ @Override
130
+
144
-
131
+ protected void onCreate(@Nullable Bundle savedInstanceState) {
132
+
133
+ super.onCreate(savedInstanceState);
134
+
135
+ setContentView(R.layout.problem_list);
136
+
137
+
138
+
139
+ //Realmの設定
140
+
141
+ mRealm = Realm.getDefaultInstance();
142
+
143
+ mRealm.addChangeListener(mRealmListener);
144
+
145
+ mGoDataRealmResults = mRealm.where(GoData.class).findAll(); //ここでdata : size = 1になった
146
+
147
+
148
+
149
+ //ListViewの設定
150
+
151
+ mListView = (ListView) findViewById(R.id.listView1);
152
+
153
+
154
+
155
+ //ListViewをタップしたときの処理
156
+
157
+ mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
158
+
159
+ @Override
160
+
161
+ public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
162
+
163
+
164
+
165
+ GoData goData = (GoData) parent.getAdapter().getItem(pos);
166
+
167
+
168
+
169
+ Intent intent = new Intent(ProblemList.this, Problem.class);
170
+
171
+ intent.putExtra(EXTRA_GODATA, goData);
172
+
173
+
174
+
175
+ startActivity(intent);
176
+
177
+ }
178
+
179
+ });
180
+
181
+ }
182
+
183
+
184
+
185
+ @Override
186
+
145
- public void onChange(Object element) {
187
+ public void onResume() {
188
+
146
-
189
+ super.onResume();
190
+
191
+
192
+
193
+ if (mGoDataAdapter == null) {
194
+
195
+ List<GoData> dataList = null;
196
+
197
+ try {
198
+
199
+ dataList = loadGoData();
200
+
201
+ } catch (IOException e) {
202
+
203
+ e.printStackTrace();
204
+
205
+ }
206
+
207
+
208
+
209
+ mGoDataAdapter = new GoDataAdapter(this);
210
+
211
+ mGoDataAdapter.setGoData(dataList);
212
+
213
+
214
+
147
- reloadListView();
215
+ //ListViewに表示
216
+
217
+ mListView.setAdapter(mGoDataAdapter);
218
+
219
+ mGoDataAdapter.notifyDataSetChanged();
148
220
 
149
221
  }
150
222
 
223
+ addGoData();
224
+
225
+
226
+
227
+ reloadListView();
228
+
151
- };
229
+ }
152
230
 
153
231
 
154
232
 
155
233
  @Override
156
234
 
235
+ protected void onDestroy() {
236
+
237
+ super.onDestroy();
238
+
239
+
240
+
241
+ mRealm.close();
242
+
243
+ }
244
+
245
+
246
+
247
+ public List<GoData> loadGoData() throws IOException {
248
+
249
+
250
+
251
+ loadJsonFromStream();
252
+
253
+
254
+
255
+ return mRealm.where(GoData.class).findAll();
256
+
257
+ }
258
+
259
+
260
+
157
- protected void onCreate(@Nullable Bundle savedInstanceState) {
261
+ private void loadJsonFromStream() throws IOException {
158
-
159
- super.onCreate(savedInstanceState);
262
+
160
-
161
- setContentView(R.layout.problem_list);
263
+
162
-
163
-
164
-
165
- // ファイルの読み込み
166
-
167
- InputStream is;
168
264
 
169
265
  BufferedReader br;
170
266
 
@@ -172,10 +268,14 @@
172
268
 
173
269
 
174
270
 
271
+ InputStream is = getAssets().open("sgfdata.json");
272
+
273
+
274
+
275
+ mRealm.beginTransaction();
276
+
175
277
  try {
176
278
 
177
- is = this.getAssets().open("sgfdata.json");
178
-
179
279
  br = new BufferedReader((new InputStreamReader(is)));
180
280
 
181
281
  String s;
@@ -186,33 +286,21 @@
186
286
 
187
287
  }
188
288
 
189
- // Json読み込み
289
+
190
290
 
191
291
  JSONObject jsonObject = new JSONObject(json);
192
292
 
193
-
194
-
195
- // 問題データ追加
196
-
197
- //JSONArray easy = problems.getJSONArray("easy");
198
-
199
293
  ZahyoChanger changer = new ZahyoChanger();
200
294
 
201
- JSONArray normal = jsonObject.getJSONObject("problems").getJSONArray("normal");
295
+ JSONArray normal_problem = jsonObject.getJSONObject("problems").getJSONArray("normal");
202
-
296
+
203
- for (int i = 0; i < normal.length(); i++) {
297
+ for (int i = 0; i < normal_problem.length(); i++) {
204
-
298
+
205
- String value1 = normal.getString(i);
299
+ String value1 = normal_problem.getString(i);
206
300
 
207
301
  changer.GoDataSeparate(value1);
208
302
 
209
- Log.d(TAG, "value1は " + value1 + " です!");
210
-
211
- }
303
+ }
212
-
213
- //答えデータ追加
214
-
215
- //JSONArray easy_answers = answers.getJSONArray("easy");
216
304
 
217
305
  JSONArray normal_answers = jsonObject.getJSONObject("answers").getJSONArray("normal");
218
306
 
@@ -222,166 +310,104 @@
222
310
 
223
311
  changer.GoDataSeparate(value2);
224
312
 
225
- Log.d(TAG, "value2は " + value2 + " です!");
313
+ mRealm.createAllFromJson(GoData.class, is);
314
+
315
+ mRealm.commitTransaction();
226
316
 
227
317
  }
228
318
 
229
319
  } catch (IOException | JSONException e) {
230
320
 
231
- e.printStackTrace();
321
+ mRealm.cancelTransaction();
322
+
323
+ } finally {
324
+
325
+ if (is != null) {
326
+
327
+ is.close();
328
+
329
+ }
232
330
 
233
331
  }
234
332
 
235
-
236
-
237
- //Realmの設定
238
-
239
- mRealm = Realm.getDefaultInstance();
240
-
241
- mGoDataRealmResults = mRealm.where(GoData.class).findAll();
242
-
243
- //mGoDataRealmResults.sort("date", Sort.DESCENDING);
244
-
245
- mRealm.addChangeListener(mRealmListener);
246
-
247
-
248
-
249
- //ListViewの設定
250
-
251
- mGoDataAdapter = new GoDataAdapter(getApplicationContext());
252
-
253
- mListView = (ListView) findViewById(R.id.listView1);
254
-
255
-
256
-
257
- //ListViewをタップしたときの処理
258
-
259
- mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
260
-
261
- @Override
262
-
263
- public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
264
-
265
-
266
-
267
- GoData goData = (GoData) parent.getAdapter().getItem(position);
268
-
269
-
270
-
271
- Intent intent = new Intent(ProblemList.this, Problem.class);
272
-
273
- intent.putExtra(EXTRA_GODATA, goData);
274
-
275
-
276
-
277
- startActivity(intent);
278
-
279
- }
333
+ }
334
+
335
+
336
+
280
-
337
+ private void reloadListView() {
338
+
339
+
340
+
341
+ ArrayList<GoData> goDataArrayList = new ArrayList<>();
342
+
343
+
344
+
345
+ int[] icons = {
346
+
347
+ R.mipmap.ic_launcher,
348
+
349
+ R.mipmap.ic_launcher
350
+
281
- });
351
+ };
282
-
283
-
284
-
352
+
353
+
354
+
285
- if(mGoDataRealmResults.size() != 0){
355
+ for (int i = 0; i < mGoDataRealmResults.size(); i++) {
286
-
287
-
288
-
356
+
357
+
358
+
289
- addGoData();
359
+ GoData data = new GoData();
360
+
361
+
362
+
363
+ data.setQNum(mGoDataRealmResults.get(i).getQNum());
364
+
365
+
366
+
367
+ goDataArrayList.add(data);
290
368
 
291
369
  }
292
370
 
293
371
 
294
372
 
373
+ mGoDataAdapter = new GoDataAdapter(this);
374
+
375
+ mGoDataAdapter.setGoData(goDataArrayList);
376
+
377
+ mListView.setAdapter(mGoDataAdapter);
378
+
295
- reloadListView();
379
+ mGoDataAdapter.notifyDataSetChanged();
296
-
380
+
297
- }
381
+ }
298
-
299
-
300
-
382
+
383
+
384
+
301
- private void reloadListView(){
385
+ private void addGoData() {
302
-
303
-
304
-
305
- ArrayList<GoData> goDataArrayList = new ArrayList<>();
386
+
306
-
307
-
308
-
387
+
388
+
309
- for(int i = 0; i < mGoDataRealmResults.size(); i++) {
389
+ if(mGoDataRealmResults.size() > 0){
390
+
391
+
392
+
393
+ mRealm.beginTransaction();
394
+
395
+
310
396
 
311
397
  GoData data = new GoData();
312
398
 
313
-
314
-
315
- data.setQNum(mGoDataRealmResults.get(i).getQNum());
399
+ data.setQNum("1");
316
-
400
+
317
- data.setTeban(mGoDataRealmResults.get(i).getTeban());
401
+ data.setTeban("b");
318
-
402
+
319
- data.setLevel(mGoDataRealmResults.get(i).getLevel());
403
+ data.setLevel("-5");
320
-
321
-
322
-
404
+
405
+
406
+
323
- goDataArrayList.add(data);
407
+ mRealm.commitTransaction();
324
408
 
325
409
  }
326
410
 
327
-
328
-
329
- mGoDataAdapter.setGoDataArrayList(goDataArrayList);
330
-
331
- mListView.setAdapter(mGoDataAdapter);
332
-
333
- mGoDataAdapter.notifyDataSetChanged();
334
-
335
- }
336
-
337
-
338
-
339
- @Override
340
-
341
- protected void onDestroy(){
342
-
343
- super.onDestroy();
344
-
345
-
346
-
347
- mRealm.close();
348
-
349
- }
350
-
351
-
352
-
353
- private void addGoData(){
354
-
355
-
356
-
357
- for(int j = 0; j < mGoDataRealmResults.size(); j++) {
358
-
359
- GoData data = new GoData();
360
-
361
-
362
-
363
- /* data.setQNum("1");
364
-
365
- data.setTeban("黒先");
366
-
367
- data.setLevel("5級");*/
368
-
369
- data.setQNum(mGoDataRealmResults.get(j).getQNum());
370
-
371
- data.setTeban(mGoDataRealmResults.get(j).getTeban());
372
-
373
- data.setLevel(mGoDataRealmResults.get(j).getLevel());
374
-
375
-
376
-
377
- mRealm.beginTransaction();
378
-
379
- mRealm.copyToRealmOrUpdate(data);
380
-
381
- mRealm.commitTransaction();
382
-
383
- }
384
-
385
411
  }
386
412
 
387
413
  }
@@ -390,102 +416,16 @@
390
416
 
391
417
 
392
418
 
393
- list_item.xml
419
+ ###試したこと
394
-
395
-
396
-
420
+
397
- ```xml
421
+ ```Java
398
-
399
- <?xml version="1.0" encoding="utf-8"?>
422
+
400
-
401
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
402
-
403
- android:orientation="vertical"
404
-
405
- android:layout_width="match_parent"
406
-
407
- android:layout_height="match_parent"
408
-
409
- android:padding="8dp">
410
-
411
-
412
-
413
- <RelativeLayout
423
+ if (convertView == null){
414
-
415
- android:layout_width="match_parent"
424
+
416
-
417
- android:layout_height="wrap_content"
418
-
419
- android:background="#ffffff"
420
-
421
- android:padding="8dp">
422
-
423
-
424
-
425
- <TextView
426
-
427
- android:id="@+id/q_num_level"
428
-
429
- android:layout_width="match_parent"
430
-
431
- android:layout_height="wrap_content"
432
-
433
- android:layout_toLeftOf="@+id/problem_icon"/>
434
-
435
-
436
-
437
- <TextView
438
-
439
- android:id="@+id/teban"
440
-
441
- android:layout_width="match_parent"
442
-
443
- android:layout_height="wrap_content"
444
-
445
- android:layout_below="@+id/q_num_level"
446
-
447
- android:layout_marginTop="4dp"
448
-
449
- android:layout_toLeftOf="@+id/problem_icon"
450
-
451
- android:textColor="#999999"
452
-
453
- android:textSize="12sp"/>
454
-
455
-
456
-
457
- <ImageView
458
-
459
- android:id="@+id/problem_icon"
460
-
461
- android:layout_width="48dp"
462
-
463
- android:layout_height="48dp"
464
-
465
- android:layout_alignParentRight="true"/>
425
+ convertView = mLayoutInflater.inflate(R.layout.list_item, null);
466
-
467
-
468
-
469
- </RelativeLayout>
470
-
471
-
472
-
473
- </LinearLayout>
474
426
 
475
427
  ```
476
428
 
477
-
478
-
479
- ###試したこと
480
-
481
- ```Java
482
-
483
- if (convertView == null){
484
-
485
- convertView = mLayoutInflater.inflate(R.layout.list_item, null);
486
-
487
- ```
488
-
489
429
  GoDataAdapterクラス内の上記のコードの(R.layout.list_item, null)は、(R.layout.simple_list_item_2, null)だったところを自作のxmlファイルに変更していますが、リストビューは表示されません。
490
430
 
491
431
  別のところに原因があるのだと思っています。