質問編集履歴

2

2021/04/04 15:26

投稿

_kei
_kei

スコア26

test CHANGED
File without changes
test CHANGED
@@ -6,11 +6,7 @@
6
6
 
7
7
 
8
8
 
9
- [リクエスト先](https://raw.githubusercontent.com/Biuni/PokemonGO-Pokedex/master/pokedex.json)
10
-
11
-
12
-
13
- このリクエストのレスポンスとして返されたnameとimgをそれぞれテキスト、画像としてRecyclerViewを用いて描画したいです。その際、リストの型をうまく書くことができません。
9
+ リクエストのレスポンスとして返されたnameとimgをそれぞれテキスト、画像としてRecyclerViewを用いて描画したいです。その際、リストの型をうまく書くことができません。
14
10
 
15
11
 
16
12
 

1

問題文の修正

2021/04/04 15:26

投稿

_kei
_kei

スコア26

test CHANGED
File without changes
test CHANGED
@@ -18,17 +18,121 @@
18
18
 
19
19
 
20
20
 
21
- コードのsuspend関数の戻り値をどう書けばよいか分かりません。
21
+ APIのsuspend関数の戻り値をどう書けばよいか分かりません。
22
+
23
+
24
+
22
-
25
+ JSONを基に作成したdataクラス
23
-
24
26
 
25
27
  ```kotlin
26
28
 
27
- interface PokemonApi {
29
+ data class Pokemon(
30
+
28
-
31
+ var id: Int = 0,
32
+
33
+ var num: String? = null,
34
+
35
+ var name: String? = null,
36
+
37
+ var img: String? = null,
38
+
39
+ var type: List<String>? = null,
40
+
41
+ var height: String? = null,
42
+
43
+ var weight: String? = null,
44
+
45
+ var candy: String? = null,
46
+
47
+ var candy_count: Int = 0,
48
+
49
+ var egg: String? = null,
50
+
51
+ var spawn_chance: Double = 0.0,
52
+
53
+ var avg_spawns: Double = 0.0,
54
+
55
+ var spawn_time: String? = null,
56
+
57
+ var multipliers: List<Double>? = null,
58
+
59
+ var weaknesses: List<String>? = null,
60
+
61
+ var next_evolution: List<Evolution>? = null,
62
+
63
+ var prev_evolution: List<Evolution>? = null
64
+
65
+ )
66
+
67
+
68
+
69
+ data class Evolution(
70
+
71
+ var num: String? = null,
72
+
73
+ var name: String? = null
74
+
75
+ )
76
+
77
+ ```
78
+
79
+
80
+
81
+ MainActivity
82
+
83
+ ```kotlin
84
+
85
+ class MainActivity : AppCompatActivity() {
86
+
87
+ private lateinit var recyclerView: RecyclerView
88
+
89
+ private lateinit var pokemonAdapter: PokemonAdapter
90
+
91
+
92
+
93
+ private lateinit var viewModel: MainViewModel
94
+
95
+
96
+
97
+ override fun onCreate(savedInstanceState: Bundle?) {
98
+
99
+ super.onCreate(savedInstanceState)
100
+
101
+ setContentView(R.layout.activity_main)
102
+
103
+
104
+
29
- @GET("pokedex.json")
105
+ val repository = Repository()
106
+
30
-
107
+ val viewModelFactory = MainViewModelFactory(repository)
108
+
109
+ viewModel = ViewModelProvider(this, viewModelFactory).get(MainViewModel::class.java)
110
+
111
+
112
+
113
+ viewModel.getPokemon()
114
+
115
+
116
+
117
+ recyclerView = findViewById(R.id.recycler_view)
118
+
119
+ recyclerView.layoutManager =
120
+
121
+ LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
122
+
123
+
124
+
31
- suspend fun getPokemon(): Response<List<Pokemon>>
125
+ viewModel.pokemons.observe(this, { response ->
126
+
127
+ if(response.isSuccessful){
128
+
129
+ pokemonAdapter = PokemonAdapter(this, response)
130
+
131
+ recyclerView.adapter = pokemonAdapter
132
+
133
+ }
134
+
135
+ })
32
136
 
33
137
  }
34
138
 
@@ -36,250 +140,86 @@
36
140
 
37
141
 
38
142
 
39
- JSON作成したdataクラ
143
+ Adapterのコンストラクタ内のpokemonListどのよう書けばよいのか分かりませんでした。型を上記のinterfaceと同じResponse<List<Pokemon>>にした場合、リトの中身を参照できず、onBindViewHolder等で怒られてしまいます。
40
144
 
41
145
  ```kotlin
42
146
 
43
- data class Pokemon(
44
-
45
- var id: Int = 0,
46
-
47
- var num: String? = null,
48
-
49
- var name: String? = null,
50
-
51
- var img: String? = null,
52
-
53
- var type: List<String>? = null,
54
-
55
- var height: String? = null,
56
-
57
- var weight: String? = null,
58
-
59
- var candy: String? = null,
60
-
61
- var candy_count: Int = 0,
62
-
63
- var egg: String? = null,
64
-
65
- var spawn_chance: Double = 0.0,
66
-
67
- var avg_spawns: Double = 0.0,
68
-
69
- var spawn_time: String? = null,
70
-
71
- var multipliers: List<Double>? = null,
72
-
73
- var weaknesses: List<String>? = null,
74
-
75
- var next_evolution: List<Evolution>? = null,
76
-
77
- var prev_evolution: List<Evolution>? = null
78
-
79
- )
80
-
81
-
82
-
83
- data class Evolution(
84
-
85
- var num: String? = null,
86
-
87
- var name: String? = null
88
-
89
- )
147
+ class PokemonAdapter(private var context: Context, private var pokemonList: List<Pokemon>) :
148
+
149
+ RecyclerView.Adapter<PokemonAdapter.PokemonHolder>() {
150
+
151
+
152
+
153
+ inner class PokemonHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
154
+
155
+ var pokeImage: ImageView
156
+
157
+ var pokeName: TextView
158
+
159
+
160
+
161
+ init {
162
+
163
+ pokeImage = itemView.findViewById(R.id.img_pokemon)
164
+
165
+ pokeName = itemView.findViewById(R.id.name_pokemon)
166
+
167
+ }
168
+
169
+ }
170
+
171
+
172
+
173
+ override fun onCreateViewHolder(
174
+
175
+ parent: ViewGroup,
176
+
177
+ viewType: Int
178
+
179
+ ): PokemonAdapter.PokemonHolder {
180
+
181
+ val view = LayoutInflater.from(parent.context).inflate(R.layout.row_pokemon, parent, false)
182
+
183
+ return PokemonHolder(view)
184
+
185
+ }
186
+
187
+
188
+
189
+ override fun onBindViewHolder(holder: PokemonAdapter.PokemonHolder, position: Int) {
190
+
191
+ holder.pokeName.text = pokemonList[position].name
192
+
193
+ Glide.with(context).load(pokemonList[position].img).into(holder.pokeImage)
194
+
195
+ }
196
+
197
+
198
+
199
+ override fun getItemCount(): Int {
200
+
201
+ return pokemonList.size
202
+
203
+ }
204
+
205
+
206
+
207
+ fun setPokemonList(newList: List<Pokemon>) {
208
+
209
+ pokemonList = newList
210
+
211
+ notifyDataSetChanged()
212
+
213
+ }
214
+
215
+
216
+
217
+ }
90
218
 
91
219
  ```
92
220
 
93
221
 
94
222
 
95
- MainActivity
96
-
97
- ```kotlin
98
-
99
- class MainActivity : AppCompatActivity() {
100
-
101
- private lateinit var recyclerView: RecyclerView
102
-
103
- private lateinit var pokemonAdapter: PokemonAdapter
104
-
105
-
106
-
107
- private lateinit var viewModel: MainViewModel
108
-
109
-
110
-
111
- override fun onCreate(savedInstanceState: Bundle?) {
112
-
113
- super.onCreate(savedInstanceState)
114
-
115
- setContentView(R.layout.activity_main)
116
-
117
-
118
-
119
- val repository = Repository()
120
-
121
- val viewModelFactory = MainViewModelFactory(repository)
122
-
123
- viewModel = ViewModelProvider(this, viewModelFactory).get(MainViewModel::class.java)
124
-
125
-
126
-
127
- viewModel.getPokemon()
128
-
129
-
130
-
131
- recyclerView = findViewById(R.id.recycler_view)
132
-
133
- recyclerView.layoutManager =
134
-
135
- LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
136
-
137
-
138
-
139
- viewModel.pokemons.observe(this, { response ->
140
-
141
- if(response.isSuccessful){
142
-
143
- pokemonAdapter = PokemonAdapter(this, response)
144
-
145
- recyclerView.adapter = pokemonAdapter
146
-
147
- }
148
-
149
- })
150
-
151
- }
152
-
153
- ```
154
-
155
- MainViewModel
156
-
157
- ```kotlin
158
-
159
- class MainViewModel(private val repository: Repository): ViewModel() {
160
-
161
-
162
-
163
- val pokemons: MutableLiveData<Response<List<Pokemon>>> = MutableLiveData()
164
-
165
-
166
-
167
- fun getPokemon(){
168
-
169
- viewModelScope.launch{
170
-
171
- val response: Response<List<Pokemon>> = repository.getPokemon()
172
-
173
- pokemons.value = response
174
-
175
- }
176
-
177
- }
178
-
179
- }
180
-
181
- ```
182
-
183
- Repository
184
-
185
- ```kotlin
186
-
187
- class Repository {
188
-
189
-
190
-
191
- suspend fun getPokemon(): Response<List<Pokemon>> {
192
-
193
- val response: Response<List<Pokemon>> = RetrofitInstance.api.getPokemon()
194
-
195
- return response
196
-
197
- }
198
-
199
- }
200
-
201
- ```
202
-
203
- Adapterのコンストラクタ内のpokemonListをどのように書けばよいのか分かりませんでした。型を上記のinterfaceと同じResponse<List<Pokemon>>にした場合、リストの中身を参照できず、onBindViewHolder等で怒られてしまいます。
204
-
205
- ```kotlin
206
-
207
- class PokemonAdapter(private var context: Context, private var pokemonList: List<Pokemon>) :
208
-
209
- RecyclerView.Adapter<PokemonAdapter.PokemonHolder>() {
210
-
211
-
212
-
213
- inner class PokemonHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
214
-
215
- var pokeImage: ImageView
216
-
217
- var pokeName: TextView
218
-
219
-
220
-
221
- init {
222
-
223
- pokeImage = itemView.findViewById(R.id.img_pokemon)
224
-
225
- pokeName = itemView.findViewById(R.id.name_pokemon)
226
-
227
- }
228
-
229
- }
230
-
231
-
232
-
233
- override fun onCreateViewHolder(
234
-
235
- parent: ViewGroup,
236
-
237
- viewType: Int
238
-
239
- ): PokemonAdapter.PokemonHolder {
240
-
241
- val view = LayoutInflater.from(parent.context).inflate(R.layout.row_pokemon, parent, false)
242
-
243
- return PokemonHolder(view)
244
-
245
- }
246
-
247
-
248
-
249
- override fun onBindViewHolder(holder: PokemonAdapter.PokemonHolder, position: Int) {
250
-
251
- holder.pokeName.text = pokemonList[position].name
252
-
253
- Glide.with(context).load(pokemonList[position].img).into(holder.pokeImage)
254
-
255
- }
256
-
257
-
258
-
259
- override fun getItemCount(): Int {
260
-
261
- return pokemonList.size
262
-
263
- }
264
-
265
-
266
-
267
- fun setPokemonList(newList: List<Pokemon>) {
268
-
269
- pokemonList = newList
270
-
271
- notifyDataSetChanged()
272
-
273
- }
274
-
275
-
276
-
277
- }
278
-
279
- ```
280
-
281
-
282
-
283
223
 
284
224
 
285
225
 
@@ -292,17 +232,7 @@
292
232
 
293
233
 
294
234
 
295
- 以下のように、data classを新しく定義してみるが、上記と同様にAdaoterをかくことができなかった。
235
+ 新しくdata classを定義してみるが、上記と同様にAdaoterをかくことができなかった。
296
-
297
- ```kotlin
298
-
299
- data class PokemonList(
300
-
301
- var pokemon: List<Pokemon>? = null
302
-
303
- )
304
-
305
- ```
306
236
 
307
237
 
308
238