自定义 Listview setonItemClickListner 在 DialogFragment 中无法使用 kotlin 中的 Viewbinding
Custom Listview setonItemClickListner is not working in DialogFragment using Viewbinding in kotlin
点击TextView打开DialogFragment。 DialogFragment 中设置的自定义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")
}
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 iten 值未在 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
}
}
尝试将 DialogListAdapter
中的 getItem
函数更改为:
override fun getItem(p0: Int): Serve = servNo[p0]
点击TextView打开DialogFragment。 DialogFragment 中设置的自定义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")
}
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 iten 值未在 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
}
}
尝试将 DialogListAdapter
中的 getItem
函数更改为:
override fun getItem(p0: Int): Serve = servNo[p0]