如何解决数组需要什么格式才能让微调器正确使用它?
我的应用程序中有一个微调器,应填充供应商列表。 Spinner 最初来自一些示例代码,并且使用示例中的固定字符串可以完美地工作。但是,就我而言,我从 API 获取值(供应商)列表。我可以看到 API 被正确调用以及 Logcat 中返回的数据。我想从这个响应中获取数据并将其用作我的微调器的对象列表。
我的问题有两个方面。是否有更直接的方法将响应转换为适配器,如果没有,我如何将读取响应转换为微调器将使用的内容?
我的意图代码如下所示:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".AddNewsupplier">
<TextView
android:id="@+id/lblsupplierName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:layout_marginTop="24dp"
android:text="supplier Name"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Spinner
android:id="@+id/spinSelectsupplier"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="@+id/lblsupplierName"
app:layout_constraintStart_toStartOf="@+id/txtsupplierName"
app:layout_constraintTop_toTopOf="@+id/lblsupplierName" />
</androidx.constraintlayout.widget.ConstraintLayout>
课程是:
class AddNewsupplier : AppCompatActivity() {
private lateinit var lblsupplierName: TextView
private lateinit var spinSelectsupplier: Spinner
var aSups: MutableList<String> = ArrayList()
var strSups: String = ""
var arrsuppliers = arrayOf("")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_add_new_supplier)
lblsupplierName = findViewById(R.id.lblsupplierName)
spinSelectsupplier = findViewById(R.id.spinSelectsupplier)
loadsupplierList()
var list_of_items = arrayOf(strSups)
if (spinSelectsupplier != null) {
val adapter = ArrayAdapter(this,android.R.layout.simple_spinner_item,list_of_items)
spinSelectsupplier.adapter = adapter
spinSelectsupplier.onItemSelectedListener = object :
AdapterView.OnItemSelectedListener {
override fun onItemSelected(parent: AdapterView<*>,view: View,position: Int,id: Long) {
Log.d("Seld: ",aSups[position].toString())
}
override fun onnothingSelected(parent: AdapterView<*>) {
// write code to perform some action
}
}
}
}
private fun loadsupplierList() {
Allsuppliers.create().getAllsuppliers("all").enqueue(object : Callback<supplierList> {
override fun onFailure(call: Call<supplierList>,t: Throwable) {
Log.d("Error :: ",t.localizedMessage!!)
t.printstacktrace()
}
override fun onResponse(
call: Call<supplierList>,response: Response<supplierList>
) {
Log.d("Allsuppliers :: ",response.body().toString())
val suppliers = response.body()?.APIResult!![0]
for (i in 0..suppliers.suppliers.size - 1) {
Log.d("Each: ",suppliers.suppliers[i].supplier_name)
aSups.add(suppliers.suppliers[i].supplier_name)
strSups += ",\"" + suppliers.suppliers[i].supplier_name + "\""
}
strSups = strSups.subSequence(1,strSups.length).toString()
Log.d("StrArr:",strSups)
Log.d("ArrElem:",aSups[1].toString())
}
})
}
}
为获取供应商而调用的 API 返回 3 个供应商,我可以在循环中看到每个供应商,因此我知道此时已完全返回响应。
我尝试将供应商附加到数组 aSup 中,但这对微调器不起作用(微调器为空),尽管我可以愉快地读取元素 1。我什至尝试构建一个字符串 (list_of_items) 给我“供应商 1”、“供应商 2”等,因为这与我从中获取代码的工作示例相同,但我仍然得到一个空数组。有人可以解释一下我做错了什么吗?
解决方法
在调用 loadSupplierList()
后尝试填充微调器。我的意思是确保 strSups
不为空。尝试在 onResponse()
方法中填充微调器。
像这样:
class AddNewSupplier : AppCompatActivity() {
private lateinit var lblSupplierName: TextView
private lateinit var spinSelectSupplier: Spinner
var aSups: MutableList<String> = ArrayList()
var strSups: String = ""
var arrSuppliers = arrayOf("")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_add_new_supplier)
lblSupplierName = findViewById(R.id.lblSupplierName)
spinSelectSupplier = findViewById(R.id.spinSelectSupplier)
loadSupplierList()
}
private fun loadSupplierList() {
AllSuppliers.create().getAllSuppliers("all").enqueue(object : Callback<SupplierList> {
override fun onFailure(call: Call<SupplierList>,t: Throwable) {
Log.d("Error :: ",t.localizedMessage!!)
t.printStackTrace()
}
override fun onResponse(
call: Call<SupplierList>,response: Response<SupplierList>
) {
Log.d("AllSuppliers :: ",response.body().toString())
val suppliers = response.body()?.APIResult!![0]
for (i in 0..suppliers.suppliers.size - 1) {
Log.d("Each: ",suppliers.suppliers[i].supplier_name)
aSups.add(suppliers.suppliers[i].supplier_name)
strSups += ",\"" + suppliers.suppliers[i].supplier_name + "\""
}
strSups = strSups.subSequence(1,strSups.length).toString()
Log.d("StrArr:",strSups)
Log.d("ArrElem:",aSups[1].toString())
// call the new method here
fillSpinner()
}
})
}
private fun fillSpinner(){
var list_of_items = arrayOf(strSups)
if (spinSelectSupplier != null) {
val adapter = ArrayAdapter(this,android.R.layout.simple_spinner_item,list_of_items)
spinSelectSupplier.adapter = adapter
spinSelectSupplier.onItemSelectedListener = object :
AdapterView.OnItemSelectedListener {
override fun onItemSelected(parent: AdapterView<*>,view: View,position: Int,id: Long) {
Log.d("Seld: ",aSups[position].toString())
}
override fun onNothingSelected(parent: AdapterView<*>) {
// write code to perform some action
}
}
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。