gpt4 book ai didi

android - 改造响应问题

转载 作者:太空狗 更新时间:2023-10-29 13:01:58 28 4
gpt4 key购买 nike

我正在尝试通过 Retrofit 获取状态列表并尝试添加 Searchable Spinner。

我得到的:

我正在获取响应中的状态列表。

我可以在 Spinner 中访问一个状态。 Picture of Json我想要什么:

我想在 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
}
}
}
}

})

波科斯:

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
}
}
}
}

})

kotlin 中的 for 循环

for (i in 0..(response.body.data.size-1)) {
stateArray.add(response.body.data.get(i).name)
}

关于android - 改造响应问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55530327/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com