Recyclerview No Adapter Attached; Skipping layout (Kotlin) How to resolve?

Asked

Viewed 155 times

0

Oops guys, I’m having this problem. I’m developing a CRUD in Kotlin and I’m having difficulties in the database reading page...

Once I enter Activity, loads the Textsviews and all the other elements but Recyclerview does not appear, so I put to debuggar and this error appears there. Someone knows what I need to do?

App Code:

die.kt

package com.nicolas.csrd

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import com.google.firebase.database.*
import kotlinx.android.synthetic.main.activity_ver_database.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class verDados : AppCompatActivity() {
    private lateinit var database: FirebaseDatabase
    private lateinit var reference: DatabaseReference

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_ver_database)

        database = FirebaseDatabase.getInstance()
        reference = database.getReference("usuarios")

        verDados()

        btn_voltar.setOnClickListener() {
            startActivity(Intent(this@verDados, Dashboard::class.java))
            finish()
        }
    }

    private fun verDados() {
        reference.addValueEventListener(object: ValueEventListener{

            override fun onCancelled(p0: DatabaseError) {
                Log.e("cancelar", p0.toString())
            }

            override fun onDataChange(p0: DataSnapshot) {
                //Colocando os usuarios numa lista
                var list = ArrayList<DatabaseModelo>()

                for (data in p0.children) {
                    val model = data.getValue(DatabaseModelo::class.java)
                    list.add(model as DatabaseModelo)
                }
                if (list.size > 0) {
                    val ususariosModelo = usuariosModelo(list)
                    recyclerview.adapter = ususariosModelo
                }

            }
        })
    }
}

Databasemodel

package com.nicolas.csrd

class DatabaseModelo() {
    lateinit var email: String
    lateinit var senha: String

    constructor(email: String, senha: String) : this() {
        this.email = email
        this.senha = senha
    }
}

users Model.kt (recyclerview Adapter)

package com.nicolas.csrd

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.view.menu.ActionMenuItemView
import androidx.appcompat.view.menu.MenuView
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.usuarios_modelo.view.*


class usuariosModelo(val list: ArrayList<DatabaseModelo>): RecyclerView.Adapter<usuariosModelo.ViewHolder>() {

    class ViewHolder(itemView: View): RecyclerView.ViewHolder(itemView) {
        val email = itemView.campo_email
        val senha = itemView.campo_senha
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.usuarios_modelo, parent, false))
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.email.text = list[position].email
        holder.senha.text = list[position].senha
    }

    override fun getItemCount(): Int {
        return list.size
    }
}

I’ll be waiting, thank you!

1 answer

0


I believe you did not define a Layoutmanager.

The Recyclerview Widget requires a layout manager, as you did not put the xml from your View try following the template below.

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layoutManager="LinearLayoutManager"/>

See that on the last line there is a layout manager called Linearlayoutmanager based on the abstract class Layoutmanager.

The second alternative is maybe your data is not being swallowed, or serialized correctly in:

            for (data in p0.children) {
                val model = data.getValue(DatabaseModelo::class.java)
                list.add(model as DatabaseModelo)
            }

If the first alternative doesn’t work, try debugging and breakpoint the list to see how the elements are.

  • Thanks, I solved based on the first answer, I put the layoutManager and it worked!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.