Kotlin 中的数据 Class
Data Class in Kotlin
我需要使用数据显示图像class
我对数据感到困惑 class 我将图像处理为图像或 Int
数据 class 用户(val pic : 图片)
或
数据 class 用户(val pic : Int)
并显示它抛出 CustomAdapter
class CustomAdapter(val userList: ArrayList<User>) : RecyclerView.Adapter<CustomAdapter.ViewHolder>() {
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomAdapter.ViewHolder {
val v = LayoutInflater.from(parent.context).inflate(R.layout.list_layout, parent, false)
return ViewHolder(v)
fun bindItems(user: User) {
val imageView = itemView.findViewById(R.id.image_item) as ImageView
imageView.setImageResource(pic: )
}
}
我知道我的代码不完整
所以我只需要示例来演示数据 Class
中的图像
数据 Class 应该有通用类型数据,但在您的代码中您试图保存 ImageView。
下面是示例数据 class:
data class User(val name: String, val pic: Int)
并在下面添加适配器代码:
class HelpAdapter : RecyclerView.Adapter<HelpAdapter.ViewHolder>() {
override fun getItemCount() = 10
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view =LayoutInflater.from(parent.context).inflate(R.layout.row_navigation_menu,parent,false)
return ViewHolder(view)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
//here add data to the views
//If your have list of user data, then you can each user like
val user = usersData[position]
holder.nameTV.text = user.name
holder.imageView.setImageResource(user.pic)
}
inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val nameTV = view.tv_name
val imageView = view.iv_pic
}
}
我需要使用数据显示图像class
我对数据感到困惑 class 我将图像处理为图像或 Int 数据 class 用户(val pic : 图片)
或 数据 class 用户(val pic : Int)
并显示它抛出 CustomAdapter
class CustomAdapter(val userList: ArrayList<User>) : RecyclerView.Adapter<CustomAdapter.ViewHolder>() {
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomAdapter.ViewHolder {
val v = LayoutInflater.from(parent.context).inflate(R.layout.list_layout, parent, false)
return ViewHolder(v)
fun bindItems(user: User) {
val imageView = itemView.findViewById(R.id.image_item) as ImageView
imageView.setImageResource(pic: )
}
}
我知道我的代码不完整 所以我只需要示例来演示数据 Class
中的图像数据 Class 应该有通用类型数据,但在您的代码中您试图保存 ImageView。 下面是示例数据 class:
data class User(val name: String, val pic: Int)
并在下面添加适配器代码:
class HelpAdapter : RecyclerView.Adapter<HelpAdapter.ViewHolder>() {
override fun getItemCount() = 10
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view =LayoutInflater.from(parent.context).inflate(R.layout.row_navigation_menu,parent,false)
return ViewHolder(view)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
//here add data to the views
//If your have list of user data, then you can each user like
val user = usersData[position]
holder.nameTV.text = user.name
holder.imageView.setImageResource(user.pic)
}
inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val nameTV = view.tv_name
val imageView = view.iv_pic
}
}