Home > Back-end >  getting the error : recyclerview No adapter attached; skipping layout
getting the error : recyclerview No adapter attached; skipping layout

Time:01-18

Here is the logcat messagesThis is main activity and i'm getting error at var userData = snaps.getValue(Users::class.java) and No adapter attached; skipping layout

    firebaseAuth = FirebaseAuth.getInstance()
    userRecyclerView = findViewById(R.id.recyclerView)
    userRecyclerView.layoutManager = LinearLayoutManager(this)
    userRecyclerView.hasFixedSize()
    userList = arrayListOf<Users>()
    getUserList()

    private fun getUserList() {
    dbref = FirebaseDatabase.getInstance().getReference("User")
    dbref.addValueEventListener(object :ValueEventListener{
        override fun onDataChange(snapshot: DataSnapshot) {
            userList.clear()
            if(snapshot.exists()){
                for (snaps in snapshot.children){
                    var userData = snaps.getValue(Users::class.java)
                    userList.add(userData!!)
                }
                val mAdapter = adapter(userList)
                userRecyclerView.adapter = mAdapter
            }
        }
        override fun onCancelled(error: DatabaseError) {
        }
    })
}

This is data class:

    class Users (
    var userId :String? =null,
    var password :String? =null
    )

This is the adapter class:

class adapter(private val userList:ArrayList<Users>)
:RecyclerView.Adapter<adapter.ViewHolder>(){
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
    val itemView =LayoutInflater.from(parent.context).inflate(R.layout.users,parent,false)
    return ViewHolder(itemView)
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    val current = userList[position]
    holder.userID.text =current.userId
}

override fun getItemCount(): Int {
    return userList.size
}
class ViewHolder(itemView:View):RecyclerView.ViewHolder(itemView){
    val userID :TextView= itemView.findViewById(R.id.userTv)
}
}

help me :(.................................. ....................................... ............................................

CodePudding user response:

This might not be the cause of your problem, but you don't need to assign adapter in getUserList(). Instead, you can do it right where you assign a LayoutManager. But to answer your question, I need more details about your error, like log messages.

CodePudding user response:

this is the problem :

var userData = snaps.getValue(Users::class.java)
userList.add(userData!!)

you're adding a string to a list of type Users.

CodePudding user response:

No adapter attached; skipping layout

The adapter is the way in which we get the list of items into the recycler view. If there is no adapter then there are no items and there is nothing to layout.

It's been a while since I used a recycler view, but I seem to remember this was a warning in logcat simply telling you that its jumped over the layout routine of the recycler view because there are no items to layout.

If you attach the adapter and call notifyDataSetChanged, then you will force the recycler view to run the layout routine, now that there are items to display.

override fun onDataChange(snapshot: DataSnapshot) {
            userList.clear()
            if(snapshot.exists()){
                for (snaps in snapshot.children){
                    var userData = snaps.getValue(Users::class.java)
                    userList.add(userData!!)
                }
                val mAdapter = adapter(userList)
                userRecyclerView.adapter = mAdapter
                mAdapter.notifyDataSetChanged()
            }
        }
  • Related