Call last three values in Firebase Database Android Studio

0

I have this code and it calls me the "Node" line that I have saved in Firebase Database

DatabaseReference fReference = database.getReference().child(Lista);
 final StringBuilder sb=new StringBuilder();
    final StringBuilder sp=new StringBuilder();
    verdosis.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            fReference.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                        sb.append(snapshot.getValue().toString());
                        sb.append(System.getProperty("line.separator") );
                    }
                    textViewf.setText(sb.toString());
                }
                @Override
                public void onCancelled(DatabaseError databaseError) {
                }
            });

            }

    });

and I just need to load where Dosis says and the last three saves marked in red, someone can help me

Thanks! I'm waiting for an answer on how I could do it

    
asked by Julian 04.10.2018 в 02:09
source

1 answer

1

Structured the information wrong, instead of keeping that data all together in a string under the pushKey you should have saved under that push key each value separately, so, in the following way you can make it much easier without using StringBuilders .

To call the last 3 values, just use limitToLast() and orderByChild() in your database reference

DatabaseReference fReference = database.getReference().child(Lista).orderByChild("dosis").limitToLast(3);

Doc: link

    
answered by 04.10.2018 в 03:41