質問編集履歴
1
追加コード
test
CHANGED
File without changes
|
test
CHANGED
@@ -8,11 +8,13 @@
|
|
8
8
|
|
9
9
|
それを使用したコードの記載方法がわかりません。
|
10
10
|
|
11
|
-
sharedpreferencesから値を設定する際のデフォルト値を設定する箇所に
|
11
|
+
sharedpreferencesから値を設定する際のデフォルト値を設定する箇所に(コード中の※の箇所)
|
12
12
|
|
13
13
|
0やspinner.setSelection(0)などを入れてみましたがエラーの連続で、、、
|
14
14
|
|
15
15
|
どなたか教えていただけませんでしょうか?
|
16
|
+
|
17
|
+
ちなみに男性で値を保存しています。
|
16
18
|
|
17
19
|
|
18
20
|
|
@@ -31,3 +33,155 @@
|
|
31
33
|
</string-array>
|
32
34
|
|
33
35
|
```
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
```
|
40
|
+
|
41
|
+
public class TestFragment extends Fragment {
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
private Spinner spinner;
|
48
|
+
|
49
|
+
private TextView Text;
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
public TestFragment() {
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
}
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
@Override
|
64
|
+
|
65
|
+
public void onCreate(Bundle savedInstanceState) {
|
66
|
+
|
67
|
+
super.onCreate(savedInstanceState);
|
68
|
+
|
69
|
+
}
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
@Override
|
74
|
+
|
75
|
+
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
76
|
+
|
77
|
+
View view = inflater.inflate(R.layout.fragment_test, container, false);
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
spinner = (Spinner) view.findViewById(R.id.spinner);
|
82
|
+
|
83
|
+
Text = (TextView) view.findViewById(R.id.Text);
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
Button save = (Button) view.findViewById(R.id.saveButton);
|
90
|
+
|
91
|
+
save.setOnClickListener(new View.OnClickListener() {
|
92
|
+
|
93
|
+
@Override
|
94
|
+
|
95
|
+
public void onClick(View v) {
|
96
|
+
|
97
|
+
saveButtonClick();
|
98
|
+
|
99
|
+
}
|
100
|
+
|
101
|
+
});
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
return view;
|
106
|
+
|
107
|
+
}
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
private void saveButtonClick() {
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
SharedPreferences sp = getActivity().getSharedPreferences("Data", Context.MODE_PRIVATE);
|
118
|
+
|
119
|
+
SharedPreferences.Editor editor = sp.edit();
|
120
|
+
|
121
|
+
editor.putInt("SaveData", spinner.getSelectedItemPosition());
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
String item = (String) spinner.getSelectedItem();
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
※Text.setText(sp.getInt("SaveData", spinner.setSelection(0)));
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
editor.commit();
|
136
|
+
|
137
|
+
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
}
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
}
|
146
|
+
|
147
|
+
```
|
148
|
+
|
149
|
+
ちなみに
|
150
|
+
|
151
|
+
```
|
152
|
+
|
153
|
+
※Text.setText(sp.getInt("SaveData", spinner.setSelection(0)));
|
154
|
+
|
155
|
+
```
|
156
|
+
|
157
|
+
このコードの場合のエラーメッセージは
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
Wrong 2nd argument type. Found: 'void', required: 'int' less...
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
getInt(String,int)
|
166
|
+
|
167
|
+
in SharedPreferences cannot be applied
|
168
|
+
|
169
|
+
to(String,void)
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
```
|
174
|
+
|
175
|
+
※Text.setText(sp.getInt("SaveData", Integer.parseInt(item)));
|
176
|
+
|
177
|
+
```
|
178
|
+
|
179
|
+
この場合ですと、
|
180
|
+
|
181
|
+
java.lang.NumberFormatException: Invalid int: "男性"
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
このように出ました。
|
186
|
+
|
187
|
+
|