teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

コードを追記

2016/10/21 06:16

投稿

intenseG
intenseG

スコア34

title CHANGED
File without changes
body CHANGED
@@ -22,10 +22,8 @@
22
22
  GoDataAdapter.java
23
23
 
24
24
  ```Java
25
- public class GoDataAdapter extends ArrayAdapter<GoData> {
26
25
 
27
- private LayoutInflater mLayoutInflater;
26
+ ------------省略----------------
28
- private ArrayList<GoData> mGoDataArrayList;
29
27
 
30
28
  public GoDataAdapter(Context context){
31
29
  super(context, 0);
@@ -56,82 +54,33 @@
56
54
  }
57
55
  ```
58
56
 
57
+ ###追記
58
+
59
59
  ProblemList.java
60
60
 
61
61
  ```Java
62
- public class ProblemList extends AppCompatActivity{
63
62
 
64
- public static final String EXTRA_GODATA = "com.example.oubeika.tsumegonomori.Problem";
63
+ ------------省略----------------
65
64
 
66
- private ListView mListView;
67
- private GoDataAdapter mGoDataAdapter;
68
- private Realm mRealm;
69
- private RealmResults<GoData> mGoDataRealmResults;
70
- private RealmChangeListener mRealmListener = new RealmChangeListener() {
71
-
72
- @Override
73
- public void onChange(Object element) {
74
- reloadListView();
75
- }
76
- };
77
-
78
65
  @Override
79
66
  protected void onCreate(@Nullable Bundle savedInstanceState) {
80
67
  super.onCreate(savedInstanceState);
81
68
  setContentView(R.layout.problem_list);
82
69
 
83
- // ファイルの読み込み
84
- InputStream is;
85
- BufferedReader br;
86
- String json = "";
87
-
88
- try {
89
- is = this.getAssets().open("sgfdata.json");
90
- br = new BufferedReader((new InputStreamReader(is)));
91
- String s;
92
- while ((s = br.readLine()) != null) {
93
- json += s;
94
- }
95
- // Json読み込み
96
- JSONObject jsonObject = new JSONObject(json);
97
-
98
- // 問題データ追加
99
- //JSONArray easy = problems.getJSONArray("easy");
100
- ZahyoChanger changer = new ZahyoChanger();
101
- JSONArray normal = jsonObject.getJSONObject("problems").getJSONArray("normal");
102
- for (int i = 0; i < normal.length(); i++) {
103
- String value1 = normal.getString(i);
104
- changer.GoDataSeparate(value1);
105
- Log.d(TAG, "value1は " + value1 + " です!");
106
- }
107
- //答えデータ追加
108
- //JSONArray easy_answers = answers.getJSONArray("easy");
109
- JSONArray normal_answers = jsonObject.getJSONObject("answers").getJSONArray("normal");
110
- for (int j = 0; j < normal_answers.length(); j++) {
111
- String value2 = normal_answers.getString(j);
112
- changer.GoDataSeparate(value2);
113
- Log.d(TAG, "value2は " + value2 + " です!");
114
- }
115
- } catch (IOException | JSONException e) {
116
- e.printStackTrace();
117
- }
118
-
119
70
  //Realmの設定
120
71
  mRealm = Realm.getDefaultInstance();
121
- mGoDataRealmResults = mRealm.where(GoData.class).findAll();
122
- //mGoDataRealmResults.sort("date", Sort.DESCENDING);
123
72
  mRealm.addChangeListener(mRealmListener);
73
+ mGoDataRealmResults = mRealm.where(GoData.class).findAll(); //ここでdata : size = 1になった
124
74
 
125
75
  //ListViewの設定
126
- mGoDataAdapter = new GoDataAdapter(getApplicationContext());
127
76
  mListView = (ListView) findViewById(R.id.listView1);
128
77
 
129
78
  //ListViewをタップしたときの処理
130
79
  mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
131
80
  @Override
132
- public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
81
+ public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
133
82
 
134
- GoData goData = (GoData) parent.getAdapter().getItem(position);
83
+ GoData goData = (GoData) parent.getAdapter().getItem(pos);
135
84
 
136
85
  Intent intent = new Intent(ProblemList.this, Problem.class);
137
86
  intent.putExtra(EXTRA_GODATA, goData);
@@ -139,102 +88,123 @@
139
88
  startActivity(intent);
140
89
  }
141
90
  });
142
-
143
- if(mGoDataRealmResults.size() != 0){
144
-
145
- addGoData();
146
- }
147
-
148
- reloadListView();
149
91
  }
150
92
 
93
+ @Override
151
- private void reloadListView(){
94
+ public void onResume() {
95
+ super.onResume();
152
96
 
97
+ if (mGoDataAdapter == null) {
153
- ArrayList<GoData> goDataArrayList = new ArrayList<>();
98
+ List<GoData> dataList = null;
99
+ try {
100
+ dataList = loadGoData();
101
+ } catch (IOException e) {
102
+ e.printStackTrace();
103
+ }
154
104
 
155
- for(int i = 0; i < mGoDataRealmResults.size(); i++) {
156
- GoData data = new GoData();
105
+ mGoDataAdapter = new GoDataAdapter(this);
106
+ mGoDataAdapter.setGoData(dataList);
157
107
 
158
- data.setQNum(mGoDataRealmResults.get(i).getQNum());
108
+ //ListViewに表示
159
- data.setTeban(mGoDataRealmResults.get(i).getTeban());
160
- data.setLevel(mGoDataRealmResults.get(i).getLevel());
109
+ mListView.setAdapter(mGoDataAdapter);
161
-
162
- goDataArrayList.add(data);
110
+ mGoDataAdapter.notifyDataSetChanged();
163
111
  }
112
+ addGoData();
164
113
 
165
- mGoDataAdapter.setGoDataArrayList(goDataArrayList);
166
- mListView.setAdapter(mGoDataAdapter);
167
- mGoDataAdapter.notifyDataSetChanged();
114
+ reloadListView();
168
115
  }
169
116
 
170
117
  @Override
171
- protected void onDestroy(){
118
+ protected void onDestroy() {
172
119
  super.onDestroy();
173
120
 
174
121
  mRealm.close();
175
122
  }
176
123
 
177
- private void addGoData(){
124
+ public List<GoData> loadGoData() throws IOException {
178
125
 
179
- for(int j = 0; j < mGoDataRealmResults.size(); j++) {
180
- GoData data = new GoData();
126
+ loadJsonFromStream();
181
127
 
182
- /* data.setQNum("1");
183
- data.setTeban("黒先");
184
- data.setLevel("5級");*/
185
- data.setQNum(mGoDataRealmResults.get(j).getQNum());
186
- data.setTeban(mGoDataRealmResults.get(j).getTeban());
187
- data.setLevel(mGoDataRealmResults.get(j).getLevel());
128
+ return mRealm.where(GoData.class).findAll();
129
+ }
188
130
 
131
+ private void loadJsonFromStream() throws IOException {
132
+
133
+ BufferedReader br;
134
+ String json = "";
135
+
136
+ InputStream is = getAssets().open("sgfdata.json");
137
+
189
138
  mRealm.beginTransaction();
139
+ try {
140
+ br = new BufferedReader((new InputStreamReader(is)));
141
+ String s;
142
+ while ((s = br.readLine()) != null) {
143
+ json += s;
144
+ }
145
+
146
+ JSONObject jsonObject = new JSONObject(json);
147
+ ZahyoChanger changer = new ZahyoChanger();
148
+ JSONArray normal_problem = jsonObject.getJSONObject("problems").getJSONArray("normal");
149
+ for (int i = 0; i < normal_problem.length(); i++) {
150
+ String value1 = normal_problem.getString(i);
151
+ changer.GoDataSeparate(value1);
152
+ }
153
+ JSONArray normal_answers = jsonObject.getJSONObject("answers").getJSONArray("normal");
154
+ for (int j = 0; j < normal_answers.length(); j++) {
155
+ String value2 = normal_answers.getString(j);
156
+ changer.GoDataSeparate(value2);
190
- mRealm.copyToRealmOrUpdate(data);
157
+ mRealm.createAllFromJson(GoData.class, is);
191
- mRealm.commitTransaction();
158
+ mRealm.commitTransaction();
159
+ }
160
+ } catch (IOException | JSONException e) {
161
+ mRealm.cancelTransaction();
162
+ } finally {
163
+ if (is != null) {
164
+ is.close();
165
+ }
192
166
  }
193
167
  }
194
- }
195
- ```
196
168
 
197
- list_item.xml
169
+ private void reloadListView() {
198
170
 
199
- ```xml
200
- <?xml version="1.0" encoding="utf-8"?>
171
+ ArrayList<GoData> goDataArrayList = new ArrayList<>();
201
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
202
- android:orientation="vertical"
203
- android:layout_width="match_parent"
204
- android:layout_height="match_parent"
205
- android:padding="8dp">
206
172
 
173
+ int[] icons = {
174
+ R.mipmap.ic_launcher,
207
- <RelativeLayout
175
+ R.mipmap.ic_launcher
208
- android:layout_width="match_parent"
209
- android:layout_height="wrap_content"
210
- android:background="#ffffff"
211
- android:padding="8dp">
176
+ };
212
177
 
213
- <TextView
214
- android:id="@+id/q_num_level"
215
- android:layout_width="match_parent"
216
- android:layout_height="wrap_content"
178
+ for (int i = 0; i < mGoDataRealmResults.size(); i++) {
217
- android:layout_toLeftOf="@+id/problem_icon"/>
218
179
 
219
- <TextView
220
- android:id="@+id/teban"
221
- android:layout_width="match_parent"
222
- android:layout_height="wrap_content"
223
- android:layout_below="@+id/q_num_level"
224
- android:layout_marginTop="4dp"
180
+ GoData data = new GoData();
225
- android:layout_toLeftOf="@+id/problem_icon"
226
- android:textColor="#999999"
227
- android:textSize="12sp"/>
228
181
 
229
- <ImageView
230
- android:id="@+id/problem_icon"
231
- android:layout_width="48dp"
232
- android:layout_height="48dp"
233
- android:layout_alignParentRight="true"/>
182
+ data.setQNum(mGoDataRealmResults.get(i).getQNum());
234
183
 
235
- </RelativeLayout>
184
+ goDataArrayList.add(data);
185
+ }
236
186
 
187
+ mGoDataAdapter = new GoDataAdapter(this);
188
+ mGoDataAdapter.setGoData(goDataArrayList);
189
+ mListView.setAdapter(mGoDataAdapter);
190
+ mGoDataAdapter.notifyDataSetChanged();
191
+ }
192
+
193
+ private void addGoData() {
194
+
195
+ if(mGoDataRealmResults.size() > 0){
196
+
237
- </LinearLayout>
197
+ mRealm.beginTransaction();
198
+
199
+ GoData data = new GoData();
200
+ data.setQNum("1");
201
+ data.setTeban("b");
202
+ data.setLevel("-5");
203
+
204
+ mRealm.commitTransaction();
205
+ }
206
+ }
207
+ }
238
208
  ```
239
209
 
240
210
  ###試したこと