Hitech P
Hitech P

Reputation: 103

How to get particular child key from firebase runtime database and set data on recyclerview android

I have a DataSnapshot with all keys of Firebase runtime database. Here is my log:-

E/MYDATA: DataSnapshot { key = Income, value = {-LgaZl_ojnqmvgpPe48D={date=2019-06-05, createdAt=2019-06-05 13:08:22, amount=333.00, caption=jjhj, from=yyyyy, time=13:08, type=ICICI Bank, updatedAt=2019-06-05 13:08:22}} }

E/MYDATA: DataSnapshot { key = createdAt, value = 2019-06-05 11:06:18 }

E/MYDATA: DataSnapshot { key = email, value = [email protected] }

Here my LOG is = MYDATA , I try to key = Income data render in Recyclerview

Here is my code :

  String id = FirebaseAuth.getInstance().getCurrentUser().getUid();

    DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Users").child(id);

    ref.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

            Log.e("MYDATA", "" + dataSnapshot);

        }

        @Override
        public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

        }

        @Override
        public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });

and here is my database structure :-

Database image

enter image description here

Look this image

Upvotes: 0

Views: 63

Answers (1)

Vedprakash Wagh
Vedprakash Wagh

Reputation: 3732

If you want to fetch the records of "Expanse", change the ref to,

DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Users").child(id).child("Expanse");

After changing it, add a ValueEventListener or ChildEventListener to the ref.

You've used ChildEventListener, so in the onChildAdded of ChildEventListener, you can get the data from Firebase like,

Expanse expanse = dataSnapshot.getValue(Expanse.class);
list.add(expanse);

Above code will come in onChildAdded of ChildEventListener, where Expanse is your POJO class in which you have getters and setters for amount, caption, date, etc.

Then you have to add this newly obtained object expanse in the List, and then notify your RecyclerViewAdapter that the data was changed.

Upvotes: 2

Related Questions