Problem displaying new activity from recyclerView using setOnClickListener Kotlin Androidx

jackpower2 :

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?

------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)
    }
}
Max :

This line startActivity(intent) in RestaurantActivity shouldn't be there.

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

}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=397581&siteId=1