單擊 TextView 打開 DialogFragment。DialogFragment 中設定的自定義 Listview。在 textview 中設定的 Listview 專案。Listview 中的資料集,但是當單擊 listview 專案時沒有任何回應,但是當我們單擊 listview 專案文本時,會給出錯誤“java.lang.ClassCastException:java.lang.Integer cannot be cast model.Serve”
MainActivity.kt
binding.tvProductServeno.setOnClickListener {
var dialogC = CustomDialog(serveNo)
dialogC.show(supportFragmentManager,"customDialog")
}
自定義對話框.kt
class CustomDialog(val servList: ArrayList<Serve>) : DialogFragment() {
private val TAG = "CustomDialog"
interface OnInputListener {
fun sendInput(input: String?)
}
var mOnInputListener: OnInputListener? = null
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val inflater = context?.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
val binding = DialogListViewBinding.inflate(inflater)
val builder = AlertDialog.Builder(requireContext())
val servAdapter = DialogListAdapter(servList, requireContext())
binding.listViewDialog.adapter = servAdapter
servAdapter.notifyDataSetChanged()
val dialog = builder.create()
dialog.show()
binding.listViewDialog.setOnItemClickListener { adapterView, view, i, l ->
val serve = adapterView.getItemAtPosition(i) as Serve
val serve1 = serve.serveNo
/* (activity as AddProduct?)..setText(input)*/
mOnInputListener?.sendInput(serve1.toString())
dialog.dismiss()
Toast.makeText(requireContext(), serve1.toString(), Toast.LENGTH_LONG).show()
}
return binding.root
}
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return super.onCreateDialog(savedInstanceState)
}
override fun onAttach(context: Context) {
super.onAttach(context)
try {
mOnInputListener = activity as OnInputListener?
} catch (e: ClassCastException) {
Log.e(TAG, "onAttach: ClassCastException: " e.message)
}
}
}
串列視圖中的資料顯示。當單擊 Listview 專案時,它不會給出任何回應。Listview item 值未在 textview 中設定,但單擊 listview 專案文本會給出錯誤 ClassCastException。
DialogListAdapter.kt
class DialogListAdapter (val servNo : ArrayList<Serve>,val context: Context) : BaseAdapter() {
override fun getCount(): Int {
return servNo.size
}
override fun getItem(p0: Int): Any {
return p0
}
override fun getItemId(p0: Int): Long {
return p0.toLong()
}
override fun getView(p0: Int, p1: View?, p2: ViewGroup?): View {
val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
val binding = DialogListItemBinding.inflate(inflater)
//val serve = getItem(p0) as Serve
val ser = servNo.get(p0)
binding.tvListViewDialogItem.text = ser.serveNo
return binding.root
}
}
uj5u.com熱心網友回復:
嘗試將getItem 函式更改DialogListAdapter為:
override fun getItem(p0: Int): Serve = servNo[p0]
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/449096.html
標籤:科特林 列表显示 android-dialogfragment 基本适配器
上一篇:Tabview滾動行為
