Aman Raj
Aman Raj

Reputation: 1

The data is not showing in the cardview from Firebase Realtime Database

I'm creating an expense tracker, The app has a login page, and after logging in there are three fragments, Dashboard, Income and Expense. Data is being stored in the database from the dashboard fragment, but I'm not able to retrieve the data from the database in a cardview and show them in the income fragment. Please Help, I'm new to Android

Here is the IncomeFragment.java file

public class IncomeFragment extends Fragment {

    //Firebase DB
    private FirebaseAuth mAuth;
    private DatabaseReference mIncomeDatabase;

    //RecyclerView
    private RecyclerView recyclerView;
    private FirebaseRecyclerAdapter adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        /**if (getArguments() != null) {
         //mParam1 = getArguments().getString(ARG_PARAM1);
         //mParam2 = getArguments().getString(ARG_PARAM2);
         }*/
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View myview = inflater.inflate(R.layout.fragment_income, container, false);

        mAuth = FirebaseAuth.getInstance();

        FirebaseUser mUser = mAuth.getCurrentUser();
        String uid = mUser.getUid();

        mIncomeDatabase = FirebaseDatabase.getInstance().getReference().child("IncomeData").child(uid);

        recyclerView = myview.findViewById(R.id.recycler_id_income);

        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());

        layoutManager.setReverseLayout(true);
        layoutManager.setStackFromEnd(true);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(layoutManager);
        return myview;
    }

    @Override
    public void onStart() {
        super.onStart();

        FirebaseRecyclerOptions<Data> options = new FirebaseRecyclerOptions.Builder<Data>()
                .setQuery(mIncomeDatabase, Data.class)
                .build();

        adapter = new FirebaseRecyclerAdapter<Data, MyViewHolder>(options) {

            public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                return new MyViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.income_recycler_data, parent, false));
            }

            protected void onBindViewHolder(MyViewHolder holder, int position, @NonNull Data model) {
                holder.setAmount(model.getAmount());
                holder.setType(model.getType());
                holder.setNote(model.getNote());
                holder.setDate(model.getDate());
            }
        };
        recyclerView.setAdapter(adapter);
    }
}


class MyViewHolder extends RecyclerView.ViewHolder {

    View mView;

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

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

    void setNote(String note) {

        TextView mNote = mView.findViewById(R.id.note_txt_income);
        mNote.setText(note);
    }

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

    void setAmount(int amount) {
        TextView mAmount = mView.findViewById(R.id.amount_txt_income);
        String stamount = String.valueOf(amount);
        mAmount.setText(stamount);
    }

}

and here is the Data.java

package dev.amanraj.expense_tracker.Model;

public class Data {

    private int amount;
    private String type;
    private String note;
    private String id;
    private String date;

    public Data(int amount, String type, String note, String id, String date) {
        this.amount = amount;
        this.type = type;
        this.note = note;
        this.id = id;
        this.date = date;
    }

    public int getAmount() {
        return amount;
    }

    public void setAmount(int amount) {
        this.amount = amount;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getNote() {
        return note;
    }

    public void setNote(String note) {
        this.note = note;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

}

It's not showing any data in the cardview

I've added Logs in the code and the logs are not showing

public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                Log.d("Adapter", "onCreateViewHolder");
                return new MyViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.income_recycler_data, parent, false));
            }

            protected void onBindViewHolder(MyViewHolder holder, int position, @NonNull Data model) {
                Log.d("Adapter", "onBindViewHolder");
                holder.setAmount(model.getAmount());
                holder.setDate(model.getDate());
                holder.setNote(model.getNote());
                holder.setType(model.getType());


            }

I'm creating two Childs in the database, as "ExpenseData"(will connect later on to the expense fragment) and as "IncomeData", the "mIncomeDatabase" is connected to "IncomeData" here's the json

{
  "ExpenseData": {
    "tBkG9wB6ILNiNnrl5c8imnX3Ch03": {
      "-NpT9gL6LPB0DY5kznb6": {
        "amount": 500,
        "date": "Jan 31, 2024",
        "id": "-NpT9gL6LPB0DY5kznb6",
        "note": "exnote",
        "type": "extype"
      },
      "-NpTABo6YNBGARNDzAJk": {
        "amount": 20,
        "date": "31-Jan-2024",
        "id": "-NpTABo6YNBGARNDzAJk",
        "note": "phonenote",
        "type": "phoneexpense"
      },
      "-NpTAel71ClSlmDOFZNT": {
        "amount": 101,
        "date": "Jan 31, 2024",
        "id": "-NpTAel71ClSlmDOFZNT",
        "note": "expensenote",
        "type": "expensetype"
      },
      "-NpTrEfhahTOlahpq_Oj": {
        "amount": 6969,
        "date": "Jan 31, 2024",
        "id": "-NpTrEfhahTOlahpq_Oj",
        "note": "check69",
        "type": "check69"
      },
      "-NpXcfymX642t3wAZ8kY": {
        "amount": 60,
        "date": "01-Feb-2024",
        "id": "-NpXcfymX642t3wAZ8kY",
        "note": "Idli Chilli",
        "type": "Food"
      },
      "-NpYCWDKErbCLUWOcNzU": {
        "amount": 300,
        "date": "01-Feb-2024",
        "id": "-NpYCWDKErbCLUWOcNzU",
        "note": "aaj daale gai",
        "type": "Petrol"
      }
    }
  },
  "IncomeData": {
    "aI2gCaEUUPZ7HGORWDIttMy3F3q2": {
      "-NpYCnXfps3Mk-3P9bUs": {
        "amount": 10000,
        "date": "01-Feb-2024",
        "id": "-NpYCnXfps3Mk-3P9bUs",
        "note": "aaj",
        "type": "salary"
      },
      "-NpYE9UIeDB88xnPZ3r2": {
        "amount": 100001,
        "date": "01-Feb-2024",
        "id": "-NpYE9UIeDB88xnPZ3r2",
        "note": "testing",
        "type": "testsalary"
      },
      "-NpYJ2ad8XesxSp-bcWL": {
        "amount": 10101,
        "date": "01-Feb-2024",
        "id": "-NpYJ2ad8XesxSp-bcWL",
        "note": "testnote",
        "type": "testnote"
      }
    },
    "tBkG9wB6ILNiNnrl5c8imnX3Ch03": {
      "-NpT9P5sfXL37veBfLwB": {
        "amount": 1000,
        "date": "Jan 31, 2024",
        "id": "-NpT9P5sfXL37veBfLwB",
        "note": "icnote",
        "type": "ictype"
      },
      "-NpTA8BJPPdeSo-SlL7x": {
        "amount": 420,
        "date": "31-Jan-2024",
        "id": "-NpTA8BJPPdeSo-SlL7x",
        "note": "phonenote",
        "type": "phonetype"
      },
      "-NpTAbSdY9kwHHfCtoQk": {
        "amount": 5500,
        "date": "Jan 31, 2024",
        "id": "-NpTAbSdY9kwHHfCtoQk",
        "note": "incomennote",
        "type": "incometype"
      },
      "-NpTlloj10OFH3A-IA82": {
        "amount": 6969,
        "date": "Jan 31, 2024",
        "id": "-NpTlloj10OFH3A-IA82",
        "note": "note69",
        "type": "check69"
      },
      "-NpU80OgryPX4nNwHCBm": {
        "amount": 10,
        "date": "31-Jan-2024",
        "id": "-NpU80OgryPX4nNwHCBm",
        "note": "aaj khaae hai",
        "type": "samosa"
      }
    }
  }
}

Upvotes: 0

Views: 37

Answers (0)

Related Questions