How do I fix my FirebaseRecyclerAdapter on a fragment?

Alberto Maggiore Agalbato :

I am working on this application and I'm trying to show my "expenses" database data in my personal fragment. I have created a recycler view file to replace the default layout. I am struggling to make this work. Can anyone help with the FirebaseRecycler Adapter and the holder class? I am not able to make this work, my fragment display the default layout with no information. I have tried and follow some codes but it is not working. Thank you

here is my personal fragment where the recycler view should appear

package uk.brighton.ama75.project;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.TextView;


import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;


import java.time.chrono.JapaneseDate;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class personalFragment extends Fragment {

    private FirebaseAuth mAuth;
    private String currentUserID;
    private DatabaseReference databaseReference;
    private RecyclerView recyclerView;
    private Adapter adapter;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View myView = inflater.inflate(R.layout.fragment_personal, container, false);


        mAuth = FirebaseAuth.getInstance();
        currentUserID = mAuth.getCurrentUser().getUid();
        databaseReference = FirebaseDatabase.getInstance().getReference().child("Expenses").child(currentUserID);

        recyclerView = myView.findViewById(R.id.recycler_expense);

        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setStackFromEnd(true);
        layoutManager.setReverseLayout(true);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(layoutManager);


        return myView;
    }



    @Override
    public void onStart() {

        super.onStart();



        Query query = FirebaseDatabase.getInstance()
                .getReference()
                .child("expenses")
                .limitToLast(5);

        FirebaseRecyclerOptions<PersonalExpenses> options =
                new FirebaseRecyclerOptions.Builder<PersonalExpenses>()
                        .setQuery(query, PersonalExpenses.class)
                        .build();


        FirebaseRecyclerAdapter adapter = new FirebaseRecyclerAdapter<PersonalExpenses, MyViewHolder>(options) {
            @Override
            protected void onBindViewHolder(@NonNull MyViewHolder holder, int position, @NonNull PersonalExpenses model) {


                holder.setDate(model.getDate());
                holder.setDescription(model.getDescription());
                holder.setAmount(model.getAmount());
                holder.setType(model.getType());

            }

            @NonNull
            @Override
            public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                View view = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.expense_recycler, parent, false);
                return new MyViewHolder(view);
            }
        };
        recyclerView.setAdapter(adapter);
    }


    @Override
    public void onStop() {

        super.onStop();
    }


   public static class MyViewHolder extends RecyclerView.ViewHolder {

        private final View mView;

        public MyViewHolder(View itemView) {
            super(itemView);
            mView = itemView;
        }

        private void setDate(String date) {
            TextView mDate = mView.findViewById(R.id.date_income);
            mDate.setText(date);
        }

        private void setType(String type) {
            TextView mType = mView.findViewById(R.id.type_txt);
            mType.setText(type);
        }

        private void setDescription(String description) {
            TextView mNote = mView.findViewById(R.id.note_txt);
            mNote.setText(description);
        }

        private void setAmount(String amount) {
            TextView mAmount = mView.findViewById(R.id.ammount_income);
            String strammount = String.valueOf(amount);
            mAmount.setText(strammount);
        }
    }
}

This is the expenses recycler view file

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/expense_recycler"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:elevation="5dp"
    app:cardElevation="5dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:orientation="horizontal">


        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1">

            <TextView
                android:id="@+id/date_income"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="6 June 2019"
                android:textAppearance="?android:textAppearanceSmall"
                android:textStyle="bold" />


        </RelativeLayout>

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2">

            <TextView
                android:id="@+id/type_txt"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:maxLines="1"
                android:text="Type"
                android:textAlignment="center"
                android:textAppearance="?android:textAppearanceSmall"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/note_txt"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/type_txt"
                android:text="@string/description"
                android:textAlignment="center"
                android:textAppearance="?android:textAppearanceSmall"
                android:textStyle="bold" />


        </RelativeLayout>

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="3
">

            <TextView
                android:id="@+id/ammount_income"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="0000"
                android:textAlignment="center"
                android:textAppearance="?android:textAppearanceSmall"
                android:textStyle="bold" />


        </RelativeLayout>

    </LinearLayout>


</androidx.cardview.widget.CardView>

This is the personal layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:background="#FFF1F3FC"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.cardview.widget.CardView
    android:layout_width="match_parent"
    app:cardElevation="5dp"
    android:elevation="10dp"
    android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="match_parent"
            android:orientation="horizontal"
            android:layout_height="wrap_content">

            <RelativeLayout
                android:layout_width="0dp"
                android:layout_weight="1"
                android:gravity="center"
                android:layout_margin="10dp"
                android:layout_height="wrap_content"
                tools:ignore="Suspicious0dp">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:textAppearanceMedium"
                    android:textColor="@android:color/black"
                    android:id="@+id/expenses"
                    android:text="expense"/>

            </RelativeLayout>

            <RelativeLayout
                android:layout_width="0dp"
                android:layout_weight="3"
                android:gravity="center"
                android:layout_margin="10dp"
                android:layout_height="wrap_content"
                tools:ignore="Suspicious0dp">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/expense_txt"
                    android:textAppearance="?android:textAppearanceMedium"
                    android:textColor="@android:color/black"
                    android:text="000.00"/>

            </RelativeLayout>

        </LinearLayout>

    </androidx.cardview.widget.CardView>

    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:id="@+id/recycler_expense"
        android:layout_height="match_parent"/>

</LinearLayout>
Hritik Gupta :

I believe you are missing these two main code.

Set adapter.startListening() inside onStart()

Set adapter.stopListening() inside onStop()

Make sure to put the code before the scope end of onStart() as you are performing a lot of operations and setting your adapter above.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I send the data fragment to fragment with FirebaseRecyclerAdapter

How do I fix my neomuttrc syntax?

How do I fix this ArrayIndexOutOfBoundsException in my hashtable?

How do I fix plotting my boxplot?

How do I fix my logo from overlapping my menu?

how do i fix my problem with my tuple in python

My CSS is not being applied to my html, how do i fix it?

How do I fix my JFrame not opening, the compiler gives me no errors, how do I fix it?

How do I communicate between alert dialog back to my fragment?

How do i make my fragment using a viewPager

How do I change my title action bar (Fragment)?

How do I implement an adapter for my listview in fragment

how do I send my date from datepickerfragment to another fragment

How do I limit the items FirebaseRecyclerAdapter pulls from the server?

How do I fix my display after Atom broke it completely?

How do I fix this syntax error on my Try and Except

How do i fix my pipe in linux socket program?

How do I fix my duplicate detection code? Java

How do i fix my code's vulnerability?

How do I fix my code about a script that stopped working?

How do I fix this manifest error in my ChueckSUR.log?

How do I fix movement in my pygame script?

How do I fix my double-buffer not drawing to screen?

How do I find a fix for my runtime int casting error?

How do i fix the angle of my mathematical triangle of squares?

How do I fix this syntax error in my game?

How do I fix the white space on my footer in mobile view

How do I fix warped walls in my raycaster?

How do I fix my progress bar from glitching?

TOP Ranking

HotTag

Archive