android - 使用 setOnClickListener Kotlin Androidx 从 recyclerView 显示新活动时出现问题
问题描述
单击视图并返回每个持有者的值时,它不会返回任何错误,我可以在调试器中看到这一点。只是没有显示新的活动。使用 Kotlin 的经验更丰富的人可以指出我的问题吗?
- - - 适配器 - - - -
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()
}
}
- - -活动 - - - -
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)
}
}
解决方案
这条线不应该在那里startActivity(intent)
。RestaurantActivity
你可以试试这个:
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
}
推荐阅读
- php - 在 MySQL 中按数字和字母和空白值排序
- linux - 如何获取 LLVM IR 中共享库的基地址?
- c# - 无法更改文件的 mime 类型
- php - 如何正确保护 api 密钥?
- java - `Set 是什么意思
>> typeSet = resolverUtil.getClasses();` 是什么意思? - laravel - Laravel 6:文件夹内的命名路由没有获取用户对象
- reactjs - Formik 两种形式之间的错误初始值
- c# - Windows 10 最大路径长度限制
- php - Laravel/Eloquent 强制执行 findOrFail 和 safeOrFail
- c++ - 班级中的渐进数字