質問編集履歴
2
内容編集
test
CHANGED
File without changes
|
test
CHANGED
@@ -36,7 +36,7 @@
|
|
36
36
|
|
37
37
|
|
38
38
|
|
39
|
-
追記ViewModelにLiveDataを使用するように変更
|
39
|
+
**追記ViewModelにLiveDataを使用するように、`AppState.kt`,`MainActivity.kt`,`ListActivity.kt`を変更**
|
40
40
|
|
41
41
|
|
42
42
|
|
1
内容編集
test
CHANGED
File without changes
|
test
CHANGED
@@ -34,6 +34,12 @@
|
|
34
34
|
|
35
35
|
### 該当のソースコード
|
36
36
|
|
37
|
+
|
38
|
+
|
39
|
+
追記ViewModelにLiveDataを使用するように変更
|
40
|
+
|
41
|
+
|
42
|
+
|
37
43
|
build.gradle
|
38
44
|
|
39
45
|
```gradle
|
@@ -132,9 +138,11 @@
|
|
132
138
|
|
133
139
|
```kotlin
|
134
140
|
|
135
|
-
package com.
|
141
|
+
package com.tomato_love.arrayvaluetest
|
142
|
+
|
143
|
+
|
144
|
+
|
136
|
-
|
145
|
+
import androidx.lifecycle.MutableLiveData
|
137
|
-
|
138
146
|
|
139
147
|
import androidx.lifecycle.ViewModel
|
140
148
|
|
@@ -148,7 +156,11 @@
|
|
148
156
|
|
149
157
|
var array = arrayOf("one","two","three")
|
150
158
|
|
151
|
-
var arrayIndex:Int = 0
|
159
|
+
// var arrayIndex:Int = 0
|
160
|
+
|
161
|
+
var arrayIndex: MutableLiveData<Int> = MutableLiveData(0)
|
162
|
+
|
163
|
+
|
152
164
|
|
153
165
|
}
|
154
166
|
|
@@ -160,7 +172,7 @@
|
|
160
172
|
|
161
173
|
```kotlin
|
162
174
|
|
163
|
-
package com.
|
175
|
+
package com.tomato_love.arrayvaluetest
|
164
176
|
|
165
177
|
|
166
178
|
|
@@ -174,10 +186,16 @@
|
|
174
186
|
|
175
187
|
import android.widget.TextView
|
176
188
|
|
189
|
+
import androidx.lifecycle.LifecycleOwner
|
190
|
+
|
191
|
+
import androidx.lifecycle.Observer
|
192
|
+
|
177
193
|
import androidx.lifecycle.ViewModelProvider
|
178
194
|
|
179
195
|
|
180
196
|
|
197
|
+
|
198
|
+
|
181
199
|
class MainActivity : AppCompatActivity() {
|
182
200
|
|
183
201
|
|
@@ -206,7 +224,13 @@
|
|
206
224
|
|
207
225
|
|
208
226
|
|
209
|
-
|
227
|
+
// text.text = appState.array[appState.arrayIndex]
|
228
|
+
|
229
|
+
|
230
|
+
|
231
|
+
appState.arrayIndex.observe({ lifecycle }, {
|
232
|
+
|
233
|
+
text.text = appState.array[it] })
|
210
234
|
|
211
235
|
|
212
236
|
|
@@ -226,6 +250,8 @@
|
|
226
250
|
|
227
251
|
|
228
252
|
|
253
|
+
|
254
|
+
|
229
255
|
```
|
230
256
|
|
231
257
|
|
@@ -308,7 +334,7 @@
|
|
308
334
|
|
309
335
|
```kotlin
|
310
336
|
|
311
|
-
package com.
|
337
|
+
package com.tomato_love.arrayvaluetest
|
312
338
|
|
313
339
|
|
314
340
|
|
@@ -368,11 +394,19 @@
|
|
368
394
|
|
369
395
|
when (item) {
|
370
396
|
|
397
|
+
// "one" -> appState.arrayIndex = 0
|
398
|
+
|
399
|
+
// "two" -> appState.arrayIndex = 1
|
400
|
+
|
401
|
+
// "three" -> appState.arrayIndex = 2
|
402
|
+
|
403
|
+
|
404
|
+
|
371
|
-
"one" -> appState.arrayIndex
|
405
|
+
"one" -> appState.arrayIndex.postValue(0)
|
372
|
-
|
406
|
+
|
373
|
-
"two" -> appState.arrayIndex
|
407
|
+
"two" -> appState.arrayIndex.postValue(1)
|
374
|
-
|
408
|
+
|
375
|
-
"three" -> appState.arrayIndex
|
409
|
+
"three" -> appState.arrayIndex.postValue(2)
|
376
410
|
|
377
411
|
}
|
378
412
|
|
@@ -390,6 +424,8 @@
|
|
390
424
|
|
391
425
|
|
392
426
|
|
427
|
+
|
428
|
+
|
393
429
|
```
|
394
430
|
|
395
431
|
|