質問編集履歴

1

MakeQuizActivityの追記、文字数制限で一部MainActivity削除

2020/06/08 02:57

投稿

j06110611
j06110611

スコア54

test CHANGED
File without changes
test CHANGED
@@ -82,143 +82,69 @@
82
82
 
83
83
  ### コード
84
84
 
85
- ```MainActivity
85
+ ```MakeQuizActivity
86
86
 
87
- public class MainActivity extends AppCompatActivity {
87
+ public class MakeQuizActivity extends AppCompatActivity {
88
88
 
89
- public static ArrayList data = new ArrayList<String>();
89
+ public static Map<String,String> mapTitle = new HashMap<>();
90
90
 
91
- private static final int REQUEST_CODE = 1;
91
+ public static Map<String,String> mapQuiz = new HashMap<>();
92
92
 
93
- private ArrayAdapter adapter;
94
-
95
- ListView listView;
96
-
97
- public static int count = 1;
98
-
99
- public static ArrayList titleList = new ArrayList<String>();
100
-
101
- public static ArrayList quizList = new ArrayList<String>();
102
-
103
- public static ArrayList answerList = new ArrayList<String>();
93
+ public static Map<String,String> mapAnswer = new HashMap<>();
104
94
 
105
95
 
106
96
 
107
97
  @Override
108
98
 
109
- protected void onActivityResult(int request_Code, int resultCode, Intent result) {
110
-
111
- switch (request_Code) {
112
-
113
- //MakeQuizActivityから戻ってきた場合
114
-
115
- case (REQUEST_CODE):
116
-
117
- //クイズを作るボタンを押して戻ってきたときの処理
118
-
119
- if (resultCode == RESULT_OK) {
120
-
121
- //titleList.add(result.getStringExtra("INPUT_TITLE"));
122
-
123
- titleList.add(PreferenceManager.sp.getString(DataKeys.TITLE.getValue() + String.valueOf(count),"nothing"));
124
-
125
- quizList.add(PreferenceManager.sp.getString(DataKeys.QUIZ.getValue() + String.valueOf(count),"nothing"));
126
-
127
- answerList.add(PreferenceManager.sp.getString(DataKeys.ANSWER.getValue() + String.valueOf(count),"nothing"));
128
-
129
- data.add(PreferenceManager.sp.getString(DataKeys.TITLE.getValue() + String.valueOf(count),"nothing"));
130
-
131
- adapter.notifyDataSetChanged();
132
-
133
- count ++;
134
-
135
- }
136
-
137
- //押されなかったときの処理
138
-
139
- else if (resultCode == RESULT_CANCELED) {
140
-
141
-
142
-
143
- }
144
-
145
- break;
146
-
147
- }
148
-
149
- }
150
-
151
-
152
-
153
99
  protected void onCreate(Bundle savedInstanceState) {
154
100
 
155
101
  super.onCreate(savedInstanceState);
156
102
 
157
- setContentView(R.layout.activity_main);
103
+ setContentView(R.layout.activity_make_quiz);
158
104
 
159
- setTitle("クイズ一覧");
105
+ setTitle("クイズ作成");
160
106
 
161
107
 
162
108
 
163
- // リスト項目とListViewを対応付けるArrayAdapterを用意する
109
+ Button button2 = (Button)findViewById(R.id.makeQuiz_Button2);
164
110
 
165
- adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_multiple_choice, data);
166
-
167
- listView = findViewById(R.id.listView);
168
-
169
- // ListViewにArrayAdapterを設定する
170
-
171
- listView.setAdapter(adapter);
172
-
173
- SharedPreferences sp = getSharedPreferences("DataStore", MODE_PRIVATE);
174
-
175
- SharedPreferences.Editor editor = sp.edit();
176
-
177
- PreferenceManager.init(sp, editor);
178
-
179
- int updateInt = PreferenceManager.sp.getInt("count", 10);
180
-
181
- for (int i = 1; i <= updateInt; i++) {
182
-
183
- if (PreferenceManager.sp.getString(DataKeys.TITLE.getValue() + String.valueOf(i), "nothing") != "nothing") {
184
-
185
- update();
186
-
187
- }
188
-
189
- }
190
-
191
-
192
-
193
- FloatingActionButton fab = findViewById(R.id.floatingActionButton);
194
-
195
- fab.setOnClickListener(new View.OnClickListener() {
111
+ button2.setOnClickListener(new View.OnClickListener() {
196
112
 
197
113
  @Override
198
114
 
199
115
  public void onClick(View v) {
200
116
 
201
- Intent intent = new Intent(MainActivity.this, com.test.quizmaker.MakeQuizActivity.class);
117
+ Intent intent = new Intent(MakeQuizActivity.this,com.test.quizmaker.MainActivity.class);
202
118
 
203
- startActivityForResult(intent, REQUEST_CODE);
119
+ String strTitle = ((EditText)findViewById(R.id.editText)).getText().toString();
204
120
 
205
- }
121
+ String strQuiz = ((EditText)findViewById(R.id.editText2)).getText().toString();
206
122
 
207
- });
123
+ String strAnswer = ((EditText)findViewById(R.id.editText3)).getText().toString();
208
124
 
125
+ PreferenceManager.saveInt(MainActivity.count);
209
126
 
127
+ PreferenceManager.saveString(strTitle, DataKeys.TITLE, String.valueOf(MainActivity.count));
210
128
 
211
- Button button3 = (Button) findViewById(R.id.quizStartButton);
129
+ //MainActivity.titleList.add(strTitle);
212
130
 
213
- button3.setOnClickListener(new View.OnClickListener() {
131
+ mapTitle.put(DataKeys.TITLE.getValue() + String.valueOf(MainActivity.count),strTitle);
214
132
 
215
- @Override
133
+ PreferenceManager.saveString(strQuiz, DataKeys.QUIZ, String.valueOf(MainActivity.count));
216
134
 
217
- public void onClick(View v) {
135
+ //MainActivity.quizList.add(strQuiz);
218
136
 
219
- Intent intent = new Intent(MainActivity.this, com.test.quizmaker.ShowQuizActivity.class);
137
+ mapQuiz.put(DataKeys.QUIZ.getValue() + String.valueOf(MainActivity.count),strQuiz);
220
138
 
139
+ PreferenceManager.saveString(strAnswer, DataKeys.ANSWER, String.valueOf(MainActivity.count));
140
+
141
+ //MainActivity.answerList.add(strAnswer);
142
+
143
+ mapAnswer.put(DataKeys.ANSWER.getValue() + String.valueOf(MainActivity.count),strAnswer);
144
+
221
- startActivity(intent);
145
+ setResult(RESULT_OK, intent);
146
+
147
+ finish();
222
148
 
223
149
  }
224
150
 
@@ -226,152 +152,8 @@
226
152
 
227
153
  }
228
154
 
229
-
230
-
231
- public void update(){
232
-
233
- int updateInt = PreferenceManager.sp.getInt("count", 10);
234
-
235
- for (int i = 1; i <= updateInt; i++) {
236
-
237
- String updateTitle = PreferenceManager.sp.getString(DataKeys.TITLE.getValue() + String.valueOf(i), "nothing");
238
-
239
- String updateQuiz = PreferenceManager.sp.getString(DataKeys.QUIZ.getValue() + String.valueOf(i), "nothing");
240
-
241
- String updateAnswer = PreferenceManager.sp.getString(DataKeys.ANSWER.getValue() + String.valueOf(i), "nothing");
242
-
243
- MakeQuizActivity.mapTitle.put(DataKeys.TITLE.getValue() + String.valueOf(i), updateTitle);
244
-
245
- MakeQuizActivity.mapQuiz.put(DataKeys.QUIZ.getValue() + String.valueOf(i), updateQuiz);
246
-
247
- MakeQuizActivity.mapAnswer.put(DataKeys.ANSWER.getValue() + String.valueOf(i), updateAnswer);
248
-
249
- titleList.add(updateTitle);
250
-
251
- quizList.add(updateQuiz);
252
-
253
- answerList.add(updateAnswer);
254
-
255
- if (updateTitle != "nothing") {
256
-
257
- data.add(updateTitle);
258
-
259
- adapter.notifyDataSetChanged();
260
-
261
- }
155
+ }
262
-
263
- }
264
-
265
- }
266
156
 
267
157
 
268
158
 
269
- //オプションメニューを作成
270
-
271
- public boolean onCreateOptionsMenu(Menu menu) {
272
-
273
- //menuにcustom_menuレイアウトを適用
274
-
275
- getMenuInflater().inflate(R.menu.custom_menu, menu);
276
-
277
- //オプションメニューを表示する場合はtrue
278
-
279
- return true;
280
-
281
- }
282
-
283
-
284
-
285
- //メニュー選択時の処理
286
-
287
- @Override
288
-
289
- public boolean onOptionsItemSelected(MenuItem menuItem) {
290
-
291
- //押されたメニューのIDで処理を振り分ける
292
-
293
- //if(menuItem.getItemId() == R.id.menuClear){
294
-
295
- switch (menuItem.getItemId()) {
296
-
297
- case R.id.menuSelect:
298
-
299
- //マップの情報を取得する
300
-
301
- SparseBooleanArray checked = listView.getCheckedItemPositions();
302
-
303
-
304
-
305
- for (int i = 0; i <= checked.size(); i++) {
306
-
307
- if (checked.valueAt(i)) {
308
-
309
- for (Map.Entry<String, String> entry : MakeQuizActivity.mapTitle.entrySet()) {
310
-
311
- if (entry.getValue().equals(titleList.get(checked.keyAt(i)))) {
312
-
313
- Log.v("Debug", "キーを取得" + entry.getKey());
314
-
315
- adapter.remove(titleList.get(checked.keyAt(i)));
316
-
317
- PreferenceManager.remove(entry.getKey());
318
-
319
- }
320
-
321
- }
322
-
323
- adapter.notifyDataSetChanged();
324
-
325
- }
326
-
327
- for (Map.Entry<String, String> entry : MakeQuizActivity.mapQuiz.entrySet()) {
328
-
329
- if (entry.getValue().equals(quizList.get(checked.keyAt(i)))) {
330
-
331
- Log.v("Debug", "キーを取得" + entry.getKey());
332
-
333
- PreferenceManager.remove(entry.getKey());
334
-
335
- }
336
-
337
- }
338
-
339
- for (Map.Entry<String, String> entry : MakeQuizActivity.mapAnswer.entrySet()) {
340
-
341
- if (entry.getValue().equals(answerList.get(checked.keyAt(i)))) {
342
-
343
- Log.v("Debug", "キーを取得" + entry.getKey());
344
-
345
- PreferenceManager.remove(entry.getKey());
346
-
347
- }
348
-
349
- }
350
-
351
- }
352
-
353
- break;
354
-
355
- case R.id.menuClear:
356
-
357
- adapter.clear();
358
-
359
- adapter.notifyDataSetChanged();
360
-
361
- PreferenceManager.clear();
362
-
363
- //MakeQuizActivity.quizList.clear();
364
-
365
- count = 1;
366
-
367
- break;
368
-
369
- }
370
-
371
- return true;
372
-
373
- }
374
-
375
- }
376
-
377
159
  ```