Jongwon
Jongwon

Reputation: 31

kotlin displaying data from Firebase database in RecyclerView in fragment

This is my first time building an application. I want to display my data in firebase realtimedatabase in recyclerview. but 'E/RecyclerView: No adapter attached; skipping layout' is on the Run chart.

I'll show you my codes in order.

at first, this is my Data class in kotlin

data class BalInputDTO(
var Id : String? = null,
var Itype: String? = null,
var Icategory: String? = null,
var ldate : String? = null,
var balance: String? = null,
var commnet: String? = null)

and then this is my adapter.kt

class BalAdapter(val context: Context, val BalList: ArrayList<BalInputDTO>) :
RecyclerView.Adapter<BalAdapter.Holder>() {

override fun onBindViewHolder(holder: Holder, position: Int) {
    holder?.bind(BalList[position], context)
}


override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): Holder {
    val view = LayoutInflater.from(context).inflate(R.layout.household_detail, parent, false)
    return Holder(view)
}

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


inner class Holder(view: View?) : RecyclerView.ViewHolder(view!!) {
    val recordCategory = view?.findViewById<TextView>(R.id.record_category)
    val recordNote = view?.findViewById<TextView>(R.id.record_note)
    val recordAmount = view?.findViewById<TextView>(R.id.record_amount)
    val recordDate = view?.findViewById<TextView>(R.id.record_date)
    val recordDeleteImageView = view?.findViewById<ImageButton>(R.id.record_delete_image_button)

    fun bind(bal: BalInputDTO, context: Context) {
        recordCategory?.text = bal.Icategory
        recordNote?.text = bal.commnet
        recordAmount?.text = bal.balance
        recordDate?.text = bal.ldate
        //  recordDeleteImageView.imageb
    }
}
}

and the last code. this is my Fragment.kt (only onCreatView part)

var fragmentView : View? = null
var firedatabase : FirebaseDatabase? = null
var BalList : ArrayList<BalInputDTO> ? = null
var ref : DatabaseReference? = null

var mRecyclerView : RecyclerView? =null

override fun onCreateView(

    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?

): View? {
     fragmentView= LayoutInflater.from(activity).inflate(R.layout.fragment_household, container, false)

     firedatabase = FirebaseDatabase.getInstance()


     mRecyclerView = fragmentView?.findViewById(R.id.household_recyclerview)
     mRecyclerView?.setHasFixedSize(true)
     mRecyclerView?.layoutManager = LinearLayoutManager(context)


    BalList = arrayListOf<BalInputDTO>()
    ref = FirebaseDatabase.getInstance().getReference("BalInput")


    ref?.addValueEventListener(object : ValueEventListener {
        override fun onCancelled(p0: DatabaseError) {
            TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
        }

        override fun onDataChange(p0: DataSnapshot) {


            if(p0!!.exists()){

                for (h in p0.children){
                    val bal = h.getValue(BalInputDTO::class.java)
                    BalList?.add(bal!!)
                }

                val adapter = BalAdapter(context!!,BalList = ArrayList<BalInputDTO>())
                mRecyclerView?.setAdapter(adapter)

            }

        }
    })

    return  fragmentView
}

and this is my database enter image description here

Please let me know if I missed something or did something wrong.

Upvotes: 1

Views: 5825

Answers (2)

r2rek
r2rek

Reputation: 2233

I'd say the real issue here is this line

 val adapter = BalAdapter(context!!,BalList = ArrayList<BalInputDTO>())

try changing it to:

 val adapter = BalAdapter(context!!,BalList)

since this is where you are adding all the elements from FB.

Upvotes: 0

Dipak Sharma
Dipak Sharma

Reputation: 976

It seems like you passing the wrong list to your adapter. Try this

val adapter = BalAdapter(context!!,BalList)

instead of

val adapter = BalAdapter(context!!,BalList = ArrayList<BalInputDTO>())

Upvotes: 1

Related Questions