android-改造响应问题

我正在尝试通过翻新获取状态列表,并尝试添加可搜索微调器.

我得到的是:

我正在得到回应的国家名单.

我可以在Spinner中访问一种状态.
android-改造响应问题
我想要的是 :

我想访问Spinner中的多个状态.

代码:

getMainApp().electAPI.getStates().enqueue(object : Callback<Responseval>{
            override fun onFailure(call: Call<Responseval>, t: Throwable) {
                Toast.makeText(this@MainActivity, t?.message, Toast.LENGTH_SHORT)
            }

            override fun onResponse(call: Call<Responseval>, response: Response<Responseval>) {
                if (response.isSuccessful!!){
                    val states = response.body()?.data
                    val stateArray = arrayListOf<String>(states?.get(0)!!.name)// Problem occur here i need to put value to get state
                    val spinner = findViewById<Spinner>(R.id.spinner)
                    val adapter = ArrayAdapter<String>(this@MainActivity, android.R.layout.simple_spinner_item, stateArray)
                    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
                    spinner.adapter = adapter
                    val options = stateArray
                    spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
                        override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) {
                            Toast.makeText(this@MainActivity, " You select >> "+options[position], Toast.LENGTH_SHORT).show();
                        }

                        override fun onNothingSelected(parent: AdapterView<*>) {

                            // sometimes you need nothing here
                        }
                    }
                }
            }

        })

Pokos:

data class Responseval(val `data`: List<Data>, val message: String, val success: Boolean)

data class Data(val created_at: String, val id: Int, val name: String, val status: Int, val updated_at: String)

解决方法:

您可以使用这样的循环.

getMainApp().electAPI.getStates().enqueue(object : Callback<Responseval>{
            override fun onFailure(call: Call<Responseval>, t: Throwable) {
                Toast.makeText(this@MainActivity, t?.message, Toast.LENGTH_SHORT)
            }

            override fun onResponse(call: Call<Responseval>, response: Response<Responseval>) {
                if (response.isSuccessful!!){
                    val states = response.body()?.data
                    var stateArray = arrayListOf<String>
                    for(i = 0; i<response.body.data.size; i++){
                        stateArray.add(response.body.data.get(i).name)
                    }
                    val spinner = findViewById<Spinner>(R.id.spinner)
                    val adapter = ArrayAdapter<String>(this@MainActivity, android.R.layout.simple_spinner_item, stateArray)
                    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
                    spinner.adapter = adapter
                    val options = stateArray
                    spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
                        override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) {
                            Toast.makeText(this@MainActivity, " You select >> "+options[position], Toast.LENGTH_SHORT).show();
                        }

                        override fun onNothingSelected(parent: AdapterView<*>) {

                            // sometimes you need nothing here
                        }
                    }
                }
            }

        })

在科特林循环

for (i in 0..(response.body.data.size-1)) {
        stateArray.add(response.body.data.get(i).name)
    }
上一篇:Retrofit2 源码解析


下一篇:Android的 – 是什么让改装比HttpURLConnection类的AsyncTask快?