gpt4 book ai didi

android - 在cardview中没有以下功能 Intent 错误

转载 作者:行者123 更新时间:2023-12-02 13:30:49 25 4
gpt4 key购买 nike

我想在我的代码中使用 Intent

我使用 cardview 然后想移动到另一个 View ,

我收到错误“没有以下功能......”

class ListResep(val listResep: ArrayList<Resep>) : RecyclerView.Adapter<ListResep.CardViewViewHolder>() {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CardViewViewHolder {
val view: View = LayoutInflater.from(parent.context).inflate(R.layout.list_resep, parent, false)
return CardViewViewHolder(view)
}

override fun onBindViewHolder(holder: CardViewViewHolder, position: Int) {
val resep = listResep[position]
Glide.with(holder.itemView.context)
.load(resep.photo)
.apply(RequestOptions().override(600, 400))
.into(holder.imgPhoto)
holder.tvName.text = resep.name
holder.tvDetail.text = resep.detail

holder.btnShare.setOnClickListener { Toast.makeText(holder.itemView.context, "Share " + listResep[holder.adapterPosition].name, Toast.LENGTH_SHORT).show()
}
holder.itemView.setOnClickListener {
val intent = Intent(context, DetailResep::class.java)
startActivity(intent)
}
}

override fun getItemCount(): Int {
return listResep.size
}

inner class CardViewViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
var imgPhoto: ImageView = itemView.findViewById(R.id.img_item_photo)
var tvName: TextView = itemView.findViewById(R.id.tv_item_name)
var tvDetail: TextView = itemView.findViewById(R.id.tv_item_detail)
var btnFavorite: Button = itemView.findViewById(R.id.btn_set_favorite)
var btnShare: Button = itemView.findViewById(R.id.btn_set_share)
var btnUnFavorite: TextView = itemView.findViewById(R.id.btn_unset_favorite)
}

最佳答案

在适配器内部创建私有(private)字段上下文,从构造函数传递它。

class ListResep(private val context: Context, val listResep: ArrayList) : RecyclerView.Adapter()


那么在你的 onBindViewHolder 中必须是:
holder.itemView.setOnClickListener { 
...
Glide.with(context)
...

val intent = Intent(context, DetailResep::class.java)
context.startActivity(intent)
}

关于android - 在cardview中没有以下功能 Intent 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61516446/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com