skip to Main Content

Can anyone help mi solve this problem? I want to show list of users in the app.

Logcat error: E/RecyclerView: No adapter attached; skipping layout

User code

data class Users(val userId : String = "",
                 val name : String = "",
                 val email : String = "") {
                 }

UsersAdapter code

class UsersAdapter(var context : Context,
                   var userList : ArrayList<Users>) : RecyclerView.Adapter<UsersAdapter.UsersViewHolder>() {

    inner class UsersViewHolder(val adapterBinding : UsersItemBinding)
        : RecyclerView.ViewHolder(adapterBinding.root){}

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): UsersViewHolder {

        val binding = UsersItemBinding.inflate(LayoutInflater.from(parent.context), parent, false)

        return UsersViewHolder(binding)

    }

    override fun onBindViewHolder(holder: UsersViewHolder, position: Int) {

        holder.adapterBinding.friendUserName.text = userList[position].name




    }

    override fun getItemCount(): Int {

        return userList.size

    }


}

MainActivity code

2

Answers


  1. As stated in your Log you haven’t attatched the adapter to your RecyclerView.
    Try this in the MainActivity onCreate():

    mainBinding.yourRecyclerView.adapter = userAdapter
    
    Login or Signup to reply.
  2. Sometimes when update you want your recylerview, you must notifyDataSetChanged().
    Example

    mainBinding.yourRecylerview.adapter = userAdapter
    userAdapter.NotifyDataSetChanged()
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search