0
votes

I am trying to populate my recyclerview with data I have in my firebase database. I am quite new to these things. I have watched few tutorials but still can't do it. This is my Expense Items class. I am using GroupieViewHolder.

class ExpenseItems(val title: String, val date:String, val value:String, val description:String, val category: String): Item<GroupieViewHolder>() {
    override fun bind(viewHolder: GroupieViewHolder, position: Int) {
        viewHolder.itemView.expenseTitleTextView.text = title
        viewHolder.itemView.expenseDateTextView.text = date
        viewHolder.itemView.expenseItemValue.text = value
        viewHolder.itemView.descriptionText.text = description
        viewHolder.itemView.expenselistCategory.text = category
    }

    override fun getLayout(): Int {
        return R.layout.expenselist
    }

}

This is my firebase code that I have. I get the information about current user expenses on logcat but I don't know how to put them on the adapter.


    private fun getCurrentUserExpenses(){
        val uid = FirebaseAuth.getInstance().currentUser!!.uid
        val reference = FirebaseDatabase.getInstance().getReference("users").child(uid).child("Expenses")
        reference.addListenerForSingleValueEvent(object : ValueEventListener{
            override fun onDataChange(snapshot: DataSnapshot) {
                val adapter = GroupAdapter<GroupieViewHolder>()
                snapshot.children.forEach{
                    Log.d("Expense items",it.toString())
                }
            }
            override fun onCancelled(error: DatabaseError) {
            }

        })

    }

This is my expenselist layout

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/expenseItem"
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:background="@drawable/expenselist_background">

    <de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/expensListImageView"
        android:layout_width="35dp"
        android:layout_height="35dp"
        android:layout_marginLeft="1dp"
        android:layout_marginTop="4dp"
        android:layout_marginBottom="8dp"
        android:src="@drawable/renting"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0" />

    <TextView
        android:id="@+id/expenseTitleTextView"
        android:layout_width="88dp"
        android:layout_height="18dp"
        android:layout_marginStart="12dp"
        android:layout_marginTop="4dp"
        android:fontFamily="@font/regular"
        android:text="Expense title"
        android:textColor="@color/white"
        android:textSize="13dp"
        app:layout_constraintStart_toEndOf="@+id/expensListImageView"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/expenseDateTextView"
        android:layout_width="wrap_content"
        android:layout_height="12dp"
        android:layout_marginStart="12dp"
        android:layout_marginTop="4dp"
        android:fontFamily="@font/poppinsextralight"
        android:text="Date"
        android:textColor="@color/white"
        android:textSize="9dp"
        app:layout_constraintStart_toEndOf="@+id/expensListImageView"
        app:layout_constraintTop_toBottomOf="@+id/expenseTitleTextView" />

    <TextView
        android:id="@+id/expenseItemValue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="@font/medium"
        android:text="$999"
        android:textColor="@color/white"
        android:textSize="13dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.465" />

    <TextView
        android:id="@+id/description"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="1dp"
        android:layout_marginTop="10dp"
        android:fontFamily="@font/regular"
        android:text="Description:"
        android:textColor="@color/white"
        android:textSize="13dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/expensListImageView" />

    <TextView
        android:id="@+id/descriptionText"
        android:layout_width="33dp"
        android:layout_height="15dp"
        android:layout_marginStart="4dp"
        android:fontFamily="@font/poppinsextralight"
        android:text="TextView"
        android:textColor="@color/white"
        android:textSize="9dp"
        app:layout_constraintBottom_toBottomOf="@+id/description"
        app:layout_constraintStart_toEndOf="@+id/description"
        app:layout_constraintTop_toTopOf="@+id/description" />

    <TextView
        android:id="@+id/expenselistCategory"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="4dp"
        android:layout_marginTop="6dp"
        android:fontFamily="@font/poppinslight"
        android:text="Category"
        android:textColor="@color/white"
        android:textSize="11dp"
        app:layout_constraintStart_toEndOf="@+id/expenseTitleTextView"
        app:layout_constraintTop_toTopOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>

Here is my database

enter image description here

Thank you in advance

EDIT

Here is what I did now. I almost fixed it but there is one small issue. Here is how my getCurrentUserExpenses function looks like.

 private fun getCurrentUserExpenses(){
        val uid = FirebaseAuth.getInstance().currentUser!!.uid
        val reference = FirebaseDatabase.getInstance().getReference("/users").child(uid).child("Expenses").child("2021").child("May")
        reference.addValueEventListener(object : ValueEventListener{
            override fun onDataChange(snapshot: DataSnapshot) {
                val adapter = GroupAdapter<GroupieViewHolder>()
                snapshot.children.forEach{
                    val expenses = it.getValue(AddedExpense::class.java)
                    Log.d("Expense items",it.toString())
                    if (expenses!=null){
                        adapter.add(ExpenseItems(expenses))
                    }
                }
                expensesRecylerView.adapter=adapter
            }
            override fun onCancelled(error: DatabaseError) {
            }
        })

    }

With this I get only the expenses from month may. I want all expenses of current user to show. When I use FirebaseDatabase.getInstance().getReference("/users").child(uid).child("Expenses") it only shows 1 item which I guess is 2021 node.

I have made changes to ExpenseItems class too

class ExpenseItems(val addedExpense:AddedExpense): Item<GroupieViewHolder>() {
    override fun bind(viewHolder: GroupieViewHolder, position: Int) {

        viewHolder.itemView.expenseTitleTextView.text = addedExpense.itemName
        viewHolder.itemView.expenseDateTextView.text = addedExpense.itemDate
        viewHolder.itemView.expenseItemValue.text = addedExpense.itemValue
        viewHolder.itemView.descriptionText.text = addedExpense.itemDesc
        viewHolder.itemView.expenselistCategory.text = addedExpense.itemCategory
    }

    override fun getLayout(): Int {
        return R.layout.expenselist
    }

}

Also here is my AddedExpense class

@Parcelize
 class AddedExpense(val itemName:String, val itemDesc:String,val itemCategory:String,val itemValue:String,val itemDate:String):Parcelable{
     constructor(): this("","","","","")
 }
1
can you share your adapter snippet as well - DinkarKumar
@dinkar_kumar I am using adapter like one in this video letsbuildthatapp.com/course_video?id=3582 he is using groupie dependency - Eren Ahmed
Can you show the adapter code snippet of yours, seeing whole video and deducing on my own will be a little difficult - DinkarKumar
@dinkar_kumar here is a little comparison I made between my code and his code mediafire.com/file/i89tctru6t1bzgo/comparision.txt/file He gets usernames to show on his recyclerview - Eren Ahmed
What exactly is the problem now? - Alex Mamo

1 Answers

0
votes

Initialize your adapter on your Activity/Fragment. You should have a Recyclerview layout in your Activity/Fragment xml layout.

For example if you have ExpensesAdapter

e.g.

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

Then on onViewCreated call in your Fragment or onCreate call in your Activity, set your initialized adapter to your recyclerview adapter

e.g.

binding.myrecyclerview.adapter = ExpensesAdapter()

or create a private var adapter in your activity or fragment

private var adapter = ExpensesAdapter()

then

binding.myrecyclerview.adapter = adapter

So on your ExpensesAdapter just create a list where you'll pass/update/set your list values