回答編集履歴
1
追加
test
CHANGED
@@ -1,3 +1,45 @@
|
|
1
1
|
count も SharedPreferences に入れては如何でしょうか.
|
2
2
|
|
3
3
|
読むときに「いくつクイズが保存されているか」は必要になりそうに思いますし.
|
4
|
+
|
5
|
+
|
6
|
+
|
7
|
+
----
|
8
|
+
|
9
|
+
|
10
|
+
|
11
|
+
button2 の OnClickListener の onClick メソッド内での保存部分のみを書きますと, 以下のようになるかと思います.
|
12
|
+
|
13
|
+
count を取り出して加算し, キーに付与してデータを保存するついでに, count も保存します.
|
14
|
+
|
15
|
+
```java
|
16
|
+
|
17
|
+
//データ取得
|
18
|
+
|
19
|
+
String strTitle = ((EditText)findViewById(R.id.editText )).getText().toString();
|
20
|
+
|
21
|
+
String strQuiz = ((EditText)findViewById(R.id.editText2)).getText().toString();
|
22
|
+
|
23
|
+
String strAnswer = ((EditText)findViewById(R.id.editText3)).getText().toString();
|
24
|
+
|
25
|
+
//番号決定
|
26
|
+
|
27
|
+
SharedPreferences sp = getSharedPreferences("DataStore", MODE_PRIVATE);
|
28
|
+
|
29
|
+
int count = sp.getInt("count", 0) + 1;
|
30
|
+
|
31
|
+
//保存
|
32
|
+
|
33
|
+
SharedPreferences.Editor editor = sp.edit();
|
34
|
+
|
35
|
+
editor.putInt("count", count);
|
36
|
+
|
37
|
+
editor.putString("title" + count, strTitle );
|
38
|
+
|
39
|
+
editor.putString("quiz" + count, strQuiz );
|
40
|
+
|
41
|
+
editor.putString("answer"+ count, strAnswer);
|
42
|
+
|
43
|
+
editor.apply();
|
44
|
+
|
45
|
+
```
|