I’m trying to retrieve data from a Firebase realtime database using a recyclerview in Kotlin but the App keeps on crushing when I load the data

My Main Activity is like this

private lateinit var database:FirebaseDatabase
private lateinit var reference: DatabaseReference

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

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

    val btnTwo: Button = findViewById(R.id.button2)
    btnTwo.setOnClickListener {
        startActivity(Intent(applicationContext, Results::class.java))
    }
    val btn: Button = findViewById(R.id.button)
    btn.setOnClickListener {
        sendData()
    }
}

private fun sendData()
{
    val name = findViewById<EditText>(R.id.et_name).text.toString().trim()
    val notes = findViewById<EditText>(R.id.et_notes).text.toString().trim()

    if (name.isNotEmpty()&&notes.isNotEmpty())
    {
        val model = DatabaseModel(name, notes)
        val id = reference.push().key

        reference.child(id!!).setValue(model)
        findViewById<EditText>(R.id.et_name).setText("")
        findViewById<EditText>(R.id.et_notes).setText("")
    }
    else{
        Toast.makeText(applicationContext, "All fields required",Toast.LENGTH_LONG).show()
    }
}

My DatabaseModel is like this

    class DatabaseModel(var name: String, var notes: String) {
    }

My Adapter is like this

class DataAdapter (private val list: ArrayList) :RecyclerView.Adapter<DataAdapter.ViewHolder>() {

class ViewHolder(itemView: View): RecyclerView.ViewHolder(itemView) {
    var nName: TextView = itemView.findViewById(R.id.tvName)
    var nNotes: TextView = itemView.findViewById(R.id.tvNotes)
}


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

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    holder.nName.text = list[position].name
    holder.nNotes.text = list[position].notes
}

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

}


My Results Activity

class Results : AppCompatActivity() {

private lateinit var mRecyclerView: RecyclerView
private lateinit var database: FirebaseDatabase
private lateinit var reference: DatabaseReference

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


    database = FirebaseDatabase.getInstance()
    reference = database.getReference("Users")
    mRecyclerView = findViewById(dataRecyclerview)

    getData()
}

private fun getData() {

    reference.addValueEventListener(object :ValueEventListener{

        override fun onDataChange(snapshot: DataSnapshot) {
            val list = ArrayList<DatabaseModel>()
                for (data in snapshot.children){
                    val model = data.getValue(DatabaseModel::class.java)
                    list.add(model as DatabaseModel)
                }
                if (list.size > 0){
                    val adapter = DataAdapter(list)
                    mRecyclerView.adapter = adapter
                }
        }

        override fun onCancelled(error: DatabaseError) {
            Log.e("cancel", error.toString())
        }
    })
}

}

Source: Android Questions

LEAVE A COMMENT