im trying to input some image to my recyclelist. but when im trying to input it,it says that my variable (avatar) is String,even though i already set it to Int in data class. but when im make another Activity,the variable "avatar" says its Int. only on ListUserAdapter.kt that it says that "avatar" is String is there anything that i should change? or im just missing something to write?
here is the code that i wrote:
ListUserAdapter.kt
class ListUserAdapter(val listUser: ArrayList<User>) : RecyclerView.Adapter<ListUserAdapter.ListViewHolder>() {
private lateinit var onItemClickCallback: OnItemClickCallback
fun setOnItemClickCallback(onItemClickCallback: OnItemClickCallback){
this.onItemClickCallback = onItemClickCallback
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ListViewHolder {
val view: View = LayoutInflater.from(parent.context).inflate(R.layout.row_user,parent, false)
return ListViewHolder(view)
}
override fun onBindViewHolder(holder: ListViewHolder, position: Int) {
val (name, username, avatar) = listUser[position]
holder.imgAvatar.setImageResource(avatar)
holder.tvName.text = name
holder.tvUserName.text = username
holder.itemView.setOnClickListener {
onItemClickCallback.onItemClicked(listUser[holder.adapterPosition])
}
}
override fun getItemCount(): Int = listUser.size
class ListViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
var imgAvatar: ImageView = itemView.findViewById(R.id.tv_imgUser)
var tvName: TextView = itemView.findViewById(R.id.tv_name)
var tvUserName: TextView = itemView.findViewById(R.id.tv_username)
}
interface OnItemClickCallback {
fun onItemClicked(user: User)
}
}
User.kt
@Parcelize
data class User(
var username: String,
var name: String,
var location: String,
var company: String,
var repository: String,
var followers: String,
var following: String,
var avatar:Int,
):Parcelable
MainActivity.kt
class MainActivity : AppCompatActivity() {
private lateinit var rvUser: RecyclerView
private val list = ArrayList<User>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
rvUser = findViewById(R.id.rv_list)
rvUser.setHasFixedSize(true)
list.addAll(listUser)
showRecyclerList()
}
private val listUser: ArrayList<User>
get() {
val dataName = resources.getStringArray(R.array.name)
val dataUsername = resources.getStringArray(R.array.username)
val dataLocation = resources.getStringArray(R.array.location)
val dataRepo = resources.getStringArray(R.array.repository)
val dataCompany = resources.getStringArray(R.array.company)
val dataFollowers = resources.getStringArray(R.array.followers)
val dataFollowing = resources.getStringArray(R.array.following)
val dataAvatar = resources.obtainTypedArray(R.array.avatar)
val listUser = ArrayList<User>()
for (i in dataName.indices) {
val user = User(
dataName[i],
dataUsername[i],
dataLocation[i],dataRepo[i],
dataCompany[i], dataFollowers[i],
dataFollowing[i],
dataAvatar.getResourceId(i, -1))
listUser.add(user)
}
return listUser
}
private fun showRecyclerList() {
if (applicationContext.resources.configuration.orientation == Configuration.ORIENTATION_LANDSCAPE) {
rvUser.layoutManager = GridLayoutManager(this, 2)
} else {
rvUser.layoutManager = LinearLayoutManager(this)
}
val listUserAdapter = ListUserAdapter(list)
rvUser.adapter = listUserAdapter
listUserAdapter.setOnItemClickCallback(object : ListUserAdapter.OnItemClickCallback {
override fun onItemClicked(user: User) {
val intent = Intent(this@MainActivity, DetailActivity::class.java)
intent.putExtra(DetailActivity.KEY_USER, user)
startActivity(intent)
showSelectedUser(user)
}
})
}
private fun showSelectedUser(user: User){
Toast.makeText(this, " Selected User: " user.username, Toast.LENGTH_SHORT).show()
}
}
row_user.xml
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_margin="16dp"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
android:elevation="6dp">
<ImageView
android:id="@ id/tv_imgUser"
android:layout_width="70dp"
android:layout_height="70dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:src="@drawable/ic_launcher_background"/>
<TextView
android:id="@ id/tv_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginStart="16dp"
tools:text="Username"
android:textColor="@android:color/black"
android:textStyle="bold"
app:layout_constraintStart_toEndOf="@id/tv_imgUser"
app:layout_constraintTop_toTopOf="@ id/tv_imgUser"/>
<TextView
android:id="@ id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
tools:text="Name"
app:layout_constraintStart_toStartOf="@ id/tv_username"
app:layout_constraintTop_toBottomOf="@id/tv_username"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
CodePudding user response:
Destructuring declarations are not based on the variable names but rather on consequential calls to the componentN()
method of the data class
.
What I mean by that is that Your code:
val (name, username, avatar) = listUser[position]
Does not mean:
val name = listUser[position].name
val username = listUser[position].username
val avatar = listUser[position].avatar
But it means:
val name = listUser[position].component1()
val username = listUser[position].component2()
val avatar = listUser[position].component3()
Which can be understood as:
val name = listUser[position].username
val username = listUser[position].name
val avatar = listUser[position].location
So the avatar
value gets the value of the location
from the listUser[position]
.
You should rather get the reference to the User
that You need from the list and basically assign the values yourself. Or You can change the order of the values of the User
data class
.
You can read on the destructuring declarations more here.