【发布时间】:2021-09-02 08:32:54
【问题描述】:
我的应用中有一个微调器,应该填充供应商列表。微调器最初来自一些示例代码,并且使用示例中的固定字符串完美地工作。但是,就我而言,我从 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”等,因为这与我从中获取代码的工作示例相同,但我仍然得到一个空数组。有人可以解释一下我做错了什么吗?
【问题讨论】:
-
嗨@timfoster,在您拨打
loadSupplierList()后尝试填充微调器。我的意思是确保strSups不为空。尝试在onResponse()方法中填充微调器。 -
在 onResponse() 方法中为微调器设置适配器。
-
完美@danialiranpour,你非常喜欢。这几天我一直在为此苦苦挣扎!我需要更改 ArrayAdapter 中的第一个参数,但除了 100%。如果您将其添加为答案,我可以将其作为答案并给您点赞
标签: android arrays kotlin spinner