Kotlin - 具有两个功能的接口,其中一个功能不起作用
Kotlin - Interface with two functions and one of the functions is not working
我有一个只有一个功能的界面,它工作正常。然后我添加了第二个函数,我想用它来发送相同的数组但发送到不同的 activity,但是在主 activity 中我收到以下错误:没有为参数传递值 'PatDischargeBtnDelegate'。这是我的代码:
界面
interface PatDataCommentDelegate {
fun patDataCommentBtnClicked(ptdata: PatData)
fun dischargePatientBtnClicked(ptdata: PatData)
}
适配器
class PatDataAdapter(val ptdata: ArrayList<PatData>, val patDataBtnDelegate: PatDataCommentDelegate,
val patDischargeBtnDelegate: PatDataCommentDelegate) :
RecyclerView.Adapter<PatDataAdapter.ViewHolder>() {
......
commentImage?.setOnClickListener {
patDataBtnDelegate.patDataCommentBtnClicked(ptdata)
}
dischargePatImg?.setOnClickListener{
patDischargeBtnDelegate.dischargePatientBtnClicked(ptdata)
}
主要Activity
class PatDataActivity : AppCompatActivity(), PatDataCommentDelegate {
....
patDataFromAdapter = PatDataAdapter(ptdatas, this) //I get the error here
override fun patDataCommentBtnClicked(ptdata: PatData) {
val commentDataActivity = Intent(this, CommentsActivity::class.java)
commentDataActivity.putExtra(DOCUMENT_ID, ptdata.documentId)
commentDataActivity.putExtra(USER_ID, userId)
commentDataActivity.putExtra(PTNAME, ptdata.ptName)
commentDataActivity.putExtra(FIN, ptdata.fin)
commentDataActivity.putExtra(ROOM, ptdata.room)
commentDataActivity.putExtra(HOSP, ptdata.hosp)
startActivity(commentDataActivity)
}
override fun dischargePatientBtnClicked(ptdata: PatData) {
val dischargePatientActivity = Intent(this, DischargeActivity::class.java)
dischargePatientActivity.putExtra(DOCUMENT_ID, ptdata.documentId)
dischargePatientActivity.putExtra(USER_ID, userId)
dischargePatientActivity.putExtra(PTNAME, ptdata.ptName)
dischargePatientActivity.putExtra(FIN, ptdata.fin)
dischargePatientActivity.putExtra(ROOM, ptdata.room)
dischargePatientActivity.putExtra(HOSP, ptdata.hosp)
startActivity(dischargePatientActivity)
}
我不确定我遗漏了什么,非常感谢您的帮助。谢谢!
问题出在第三个参数中。
class PatDataAdapter(
val ptdata: ArrayList<PatData>,
val patDataBtnDelegate: PatDataCommentDelegate,
val patDischargeBtnDelegate: PatDataCommentDelegate // you don't need this one.
) : RecyclerView.Adapter<PatDataAdapter.ViewHolder>() {
这应该有效:
class PatDataAdapter(val ptdata: ArrayList<PatData>, val patDataBtnDelegate: PatDataCommentDelegate) :
RecyclerView.Adapter<PatDataAdapter.ViewHolder>() {
commentImage?.setOnClickListener {
patDataBtnDelegate.patDataCommentBtnClicked(ptdata)
}
dischargePatImg?.setOnClickListener{
patDataBtnDelegate.dischargePatientBtnClicked(ptdata)
}
您只需要引用接口 PatDataCommentDelegate
本身,而不是分别引用每个方法。
我有一个只有一个功能的界面,它工作正常。然后我添加了第二个函数,我想用它来发送相同的数组但发送到不同的 activity,但是在主 activity 中我收到以下错误:没有为参数传递值 'PatDischargeBtnDelegate'。这是我的代码:
界面
interface PatDataCommentDelegate {
fun patDataCommentBtnClicked(ptdata: PatData)
fun dischargePatientBtnClicked(ptdata: PatData)
}
适配器
class PatDataAdapter(val ptdata: ArrayList<PatData>, val patDataBtnDelegate: PatDataCommentDelegate,
val patDischargeBtnDelegate: PatDataCommentDelegate) :
RecyclerView.Adapter<PatDataAdapter.ViewHolder>() {
......
commentImage?.setOnClickListener {
patDataBtnDelegate.patDataCommentBtnClicked(ptdata)
}
dischargePatImg?.setOnClickListener{
patDischargeBtnDelegate.dischargePatientBtnClicked(ptdata)
}
主要Activity
class PatDataActivity : AppCompatActivity(), PatDataCommentDelegate {
....
patDataFromAdapter = PatDataAdapter(ptdatas, this) //I get the error here
override fun patDataCommentBtnClicked(ptdata: PatData) {
val commentDataActivity = Intent(this, CommentsActivity::class.java)
commentDataActivity.putExtra(DOCUMENT_ID, ptdata.documentId)
commentDataActivity.putExtra(USER_ID, userId)
commentDataActivity.putExtra(PTNAME, ptdata.ptName)
commentDataActivity.putExtra(FIN, ptdata.fin)
commentDataActivity.putExtra(ROOM, ptdata.room)
commentDataActivity.putExtra(HOSP, ptdata.hosp)
startActivity(commentDataActivity)
}
override fun dischargePatientBtnClicked(ptdata: PatData) {
val dischargePatientActivity = Intent(this, DischargeActivity::class.java)
dischargePatientActivity.putExtra(DOCUMENT_ID, ptdata.documentId)
dischargePatientActivity.putExtra(USER_ID, userId)
dischargePatientActivity.putExtra(PTNAME, ptdata.ptName)
dischargePatientActivity.putExtra(FIN, ptdata.fin)
dischargePatientActivity.putExtra(ROOM, ptdata.room)
dischargePatientActivity.putExtra(HOSP, ptdata.hosp)
startActivity(dischargePatientActivity)
}
我不确定我遗漏了什么,非常感谢您的帮助。谢谢!
问题出在第三个参数中。
class PatDataAdapter(
val ptdata: ArrayList<PatData>,
val patDataBtnDelegate: PatDataCommentDelegate,
val patDischargeBtnDelegate: PatDataCommentDelegate // you don't need this one.
) : RecyclerView.Adapter<PatDataAdapter.ViewHolder>() {
这应该有效:
class PatDataAdapter(val ptdata: ArrayList<PatData>, val patDataBtnDelegate: PatDataCommentDelegate) :
RecyclerView.Adapter<PatDataAdapter.ViewHolder>() {
commentImage?.setOnClickListener {
patDataBtnDelegate.patDataCommentBtnClicked(ptdata)
}
dischargePatImg?.setOnClickListener{
patDataBtnDelegate.dischargePatientBtnClicked(ptdata)
}
您只需要引用接口 PatDataCommentDelegate
本身,而不是分别引用每个方法。