自定义列表视图 setonItemClickListner 无法在 DialogFragment 中使用 kotlin 中的视图绑定工作
单击 TextView 打开 DialogFragment。 DialogFragment 中设置的自定义 Listview。在文本视图中设置列表视图项目。在 Listview 中设置数据,但是当单击 listview 项时没有任何响应,但是当我们单击 listview 项文本时出现错误“java.lang.ClassCastException:java.lang.Integer 无法转换 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)
}
}
}
Data在列表视图中显示。当单击Listview项目时,它没有给出任何响应。列表视图iten值未在文本视图中设置,但单击列表视图项目文本会出现错误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
}
}
Click on TextView open DialogFragment. Custom Listview set in DialogFragment. Listview item set in textview. Data set in Listview but when click listview item is not any response but when we click on listview item text then give error "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)
}
}
}
Data Display in Listview. When Click on Listview item then it's not give any response. Listview iten value is not set in textview but click on listview item text is give error 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
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试将
DialogListAdapter
中的getItem
函数更改为:Try to change the
getItem
function in theDialogListAdapter
to: