我有一个微调器,可以从服务器加载动态数据。微调器中的每个项目也具有自定义布局。 我设法在微调器中显示数据。有人可以帮助我获得所选的值吗?
我尝试了spinner.getItemIdAtPosition(position),但是即使单击任何项目,我的结果也仍为0。
这是我的适配器代码:
class CustomDropDownAdapter(val context: Context, var batchList: Array<BatchList>) : BaseAdapter() {
val mInflater: LayoutInflater = LayoutInflater.from(context)
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
val view: View
val vh: ItemRowHolder
if (convertView == null) {
view = mInflater.inflate(R.layout.batch_row_spinner, parent, false)
vh = ItemRowHolder(view)
view?.tag = vh
} else {
view = convertView
vh = view.tag as ItemRowHolder
}
vh.date.text = batchList.get(position).expiry_date
vh.availQty.text = "Available: ${batchList.get(position).available_quantity}"
return view
}
override fun getItem(position: Int): Any? {
return null
}
override fun getItemId(position: Int): Long {
return 0
}
override fun getCount(): Int {
return batchList.size
}
private class ItemRowHolder(row: View?) {
val date: TextView
val availQty: TextView
init {
this.date = row?.findViewById(R.id.date) as TextView
this.availQty = row?.findViewById(R.id.available) as TextView
}
}
}
这是我在微调器中加载的功能
/**Method to load all items in spinner */
private fun loadBatch(medicineId:String,pharmaId:String)
{
val call=RetrofitClient.instance.api.displayBatchList("Bearer $token",20.toString(),medicineId,0.toString(),pharmaId)
call.enqueue(object :Callback<Array<BatchList>>{
override fun onResponse(call: Call<Array<BatchList>>, response: Response<Array<BatchList>>) {
if(response.code()==200)
{
var spinnerAdapter: CustomDropDownAdapter = CustomDropDownAdapter(context!!, response.body()!!)
updateMedView.pharmaSpinerbatch.adapter = spinnerAdapter
}
}
override fun onFailure(call: Call<Array<BatchList>>, t: Throwable) {
Log.e("Batch Load error",t.message)
}
})
}
我需要获取选定的物品
答案 0 :(得分:0)
为微调器添加列表器
//item selected listener for spinner
mySpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onNothingSelected(p0: AdapterView<*>?) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun onItemSelected(p0: AdapterView<*>?, p1: View?, p2: Int, p3: Long) {
Toast.makeText(this@MainActivity, myStrings[p2], LENGTH_LONG).show()
}
}