0

I want to get last "current unit" of unique customer from Firebase database.I'm new to here. Please help me. Here is my database snapshot:

Firabase Database

I tried to with this code. But not value pass to textview. I don't know where is the error.Not Showing any errors.

 databaseBills = FirebaseDatabase.getInstance().getReference("bills");
    Query lastQuery = databaseBills.child("bills").orderByKey().limitToLast(1);
    lastQuery.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

            String value = dataSnapshot.child("current_units").getValue(String.class);

            textView17.setText(value);
        }

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

        }
    });
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
hesh
  • 1
  • 3

2 Answers2

1

You aren't getting something because you are using a wrong query. You need to remove .child("bills") call because under bills node there isn't another bills child. So your query should look like this:

Query lastQuery = databaseBills.orderByKey().limitToLast(1);
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Yeah thanx. I understood that. I tried also with this code,but not pass value to textview. – hesh Mar 07 '19 at 14:27
  • Why do you say that? Have you also tried to iterate over `dataSnapshot.getChildren()` as Frank van Puffelen mentioned in his answer? – Alex Mamo Mar 07 '19 at 14:34
0

When you execute a query against the Firebase Database, there will potentially be multiple results. So the snapshot contains a list of those results. Even if there is only a single result, the snapshot will contain a list of one result.

Your code needs to handle that list by iterating over dataSnapshot.getChildren():

public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
  for (DataSnapshot childSnapshot: dataSnapshot.getChildren()) {
    String value = childSnapshot.child("current_units").getValue(String.class);
    Log.d("onDataChange", "current_units="+value);
    textView17.setText(value);
  }
}

I highly recommend always adding logging statements like the one above to your code when troubleshooting. If the log shows a value but the UI doesn't, you know the problem is not in reading from the database. If the log doesn't show a value, you know the problem is in reading from the database. Either way: you've reduced the problem space, and can ask a more targeted question.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • hi Frank van, can you help me to pass "textView17" value outside of listener? – hesh Mar 18 '19 at 07:05
  • The up to date value will only be available inside the `onDataChange()`. – Frank van Puffelen Mar 18 '19 at 14:07
  • How can I pass that value outside of listener? Is there any method? – hesh Mar 19 '19 at 04:52
  • You can **pass** the value to another method. But calling that method must happen from with `onDataChange()`. See https://stackoverflow.com/questions/50434836/getcontactsfromfirebase-method-return-an-empty-list/50435519#50435519 for an example. – Frank van Puffelen Mar 19 '19 at 13:01