简体   繁体   English

使用 setOnClickListener Kotlin Androidx 从 recyclerView 显示新活动时出现问题

[英]Problem displaying new activity from recyclerView using setOnClickListener Kotlin Androidx

It is not returning any errors when the view is clicked and the values of each holder are being returned, I can see that in the debugger.单击视图并返回每个持有者的值时,它不会返回任何错误,我可以在调试器中看到这一点。 Just the new Activity is not being displayed.只是没有显示新的活动。 Can someone`more experienced using Kotlin point out my problem?使用 Kotlin 的经验丰富的人可以指出我的问题吗?

------Adapter------- - - - 适配器 - - - -

package org.wit.grubmobileapp

class RestaurantAdapter(val context: Context, var restaurants: List<YelpRestaurant>):
    RecyclerView.Adapter<RestaurantAdapter.ViewHolder>() {

    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        fun bind(restaurant: YelpRestaurant) {
            itemView.tvName.text = restaurant.name
            itemView.ratingBar.rating = restaurant.rating.toFloat()
            itemView.tvReviews.text = "${restaurant.numReviews} Reviews"
            itemView.tvAddress.text = restaurant.location.address
            itemView.tvCategory.text = restaurant.categories[0].title
            itemView.tvPrice.text = restaurant.price
            Glide.with(context).load(restaurant.imageUrl).into(itemView.imageView)
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_restaurant, parent, false))
    }

    override fun getItemCount() =
        restaurants.size

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val restaurant = restaurants[position]
        holder.bind(restaurant)

        holder.itemView.setOnClickListener{
            val model = restaurants.get(position)

            var rName : String = model.name
            var rPrice : String = model.price
            var rCategory : String = model.categories[0].title
            var rLocation : String = model.location.address

            val intent = Intent(context, RestaurantActivity::class.java)

            intent.putExtra("iName", rName)
            intent.putExtra("iPrice", rPrice)
            intent.putExtra("iCategory", rCategory)
            intent.putExtra("iLocation", rLocation)

            context.startActivity(intent)
        }
    }

    fun updateList(list: MutableList<YelpRestaurant>) {
        restaurants = list
        notifyDataSetChanged()
    }
}

-----Activity------- - - -活动 - - - -

package org.wit.grubmobileapp

class RestaurantActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_restaurant)

        var intent = intent

        val aName = intent.getStringExtra("iName")
        val aPrice = intent.getStringExtra("iPrice")
        val aCategory = intent.getStringExtra("iCategory")
        val aLocation = intent.getStringExtra("iLocation")

        r_name.text = aName
        r_price.text = aPrice
        r_category.text = aCategory
        r_address.text = aLocation

        startActivity(intent)
    }
}

This line startActivity(intent) in RestaurantActivity shouldn't be there. RestaurantActivity中的这一行startActivity(intent)不应该存在。

You can try this:你可以试试这个:

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_restaurant)

val aName = intent.getStringExtra("iName")
val aPrice = intent.getStringExtra("iPrice")
val aCategory = intent.getStringExtra("iCategory")
val aLocation = intent.getStringExtra("iLocation")

r_name.text = aName
r_price.text = aPrice
r_category.text = aCategory
r_address.text = aLocation

}
              **------- CustomAdapter -------**

class CustomAdapter (val activity: Activity, val inside: ArrayList ): RecyclerView.Adapter<CustomAdapter.MyViewHolder>(){ class CustomAdapter(val 活动:Activity,内部 val:ArrayList):RecyclerView.Adapter<CustomAdapter.MyViewHolder>(){

 class MyViewHolder(view: View) : RecyclerView.ViewHolder(view){
    val image: ImageView = view.findViewById(R.id.iv)
    val text: TextView = view.findViewById(R.id.tv)
    val card: CardView = view.findViewById(R.id.cv)

     
 }

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomAdapter.MyViewHolder {

    val view = activity.layoutInflater.inflate(R.layout.click_item, parent,false)
    val mvh = MyViewHolder(view)
    return mvh
}

override fun onBindViewHolder(holder: CustomAdapter.MyViewHolder, position: Int) {
    holder.text.text =inside.get(position).name
    holder.image.setImageResource(inside.get(position).picture)

    holder.card.setOnClickListener {
        var i = Intent (activity,FourSeason::class.java)
        i.putExtra("in",inside.get(position).name)
    holder.card.context.startActivity(i)}

}


override fun getItemCount() =  inside.size

} }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 androidx.recyclerview.widget.RecyclerView 从 kotlin 的回收器视图中获取新活动? - How get a new activity from recycler view in kotlin using androidx.recyclerview.widget.RecyclerView? Kotlin RecyclerView 开始新活动 - Kotlin RecyclerView start new activity RecyclerView setOnClickListener applicationContext问题 - RecyclerView setOnClickListener applicationContext problem 单击项目时将数据从 recyclerView 传递到新活动 Kotlin - Pass data from recyclerView to new activity when an item is clicked Kotlin Androidx RecyclerView 中的 ViewHolder 问题 - Problem With ViewHolder In Androidx RecyclerView 如何使用Kotlin从RecyclerView适配器类完成活动 - How to finish an activity from RecyclerView adapter class using Kotlin 使用 Kotlin 从 RecyclerView Adapter 调用 Activity 方法 - Call Activity method from RecyclerView Adapter using Kotlin 将数据从recyclerView适配器发送到新活动时出现问题 - Getting problem in sending data from recyclerView adapter to a new activity 如何在 Kotlin 中使用不同的条件/参数从 RecyclerView 的 Detail Activity 中打开一个新活动 - How to open a new activity inside the Detail Activity from RecyclerView with different conditions/parameters in Kotlin 如何在androidx.recyclerview.widget中使用androidx.recyclerview.selection。 或者如何在Android中使用Kotlin在recyclerview中选择项目? - How to use androidx.recyclerview.selection in androidx.recyclerview.widget. Or how to select an item in recyclerview using kotlin in android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM