0

This is my first question here :)

This is my Firebase:

{
  "Users": {
    {
      "G2u5-1yQI-GaKN" : {
        "login" : "Tom",
        "name" : "Tom",
        "password" : "1234"
      },
      "JMtk-T2AZ-LJY8" : {
        "login" : "Ann",
        "name" : "Ann",
        "password" : "abcd"
      }
    }
  }
}

My Firebase

I want to get value of name key of JMtk-T2AZ-LJY8 user (Ann) and set it as text of TextView in my app widget.

My code is:

final RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.my_widget);

final DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child("Users").child("JMtk-T2AZ-LJY8");

views.setTextViewText(R.id.textView2, String.valueOf(reference.child("name").getValue()));

I cannot use getValue() method here, i don't know why?

When I use similar code in onCreate method of MainActivity, it works fine (it returns "Ann:), but not in app widget. What is the solution?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Phoenix
  • 3
  • 2

1 Answers1

1

As Doug said, you're not actually loading any data yet. The code you now have, merely defines a reference to a location in your database. To get the actual data from that location, you'll need to attach a listener to the reference.

Something like:

final RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.my_widget);

final DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child("Users").child("JMtk-T2AZ-LJY8");
reference.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
      views.setTextViewText(
        R.id.textView2, 
        dataSnapshot.child("name").getValue(String.class)
      );
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        throw databaseError.toException();
    }
}

If you only need the name of the user, you can also attach your listener to just that property and save some bandwidth:

final DatabaseReference reference = FirebaseDatabase.getInstance().getReference()
                                       .child("Users/JMtk-T2AZ-LJY8/name");

reference.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
      views.setTextViewText(
        R.id.textView2, 
        dataSnapshot.getValue(String.class)
      );
    }
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • I tried it, but also in this case, I cannot use getValue() method here. The editor colors it in red. – Phoenix Mar 29 '20 at 17:06
  • In your code: reference.getValue(String.class) - I've changed reference to dataSnapshot and the result is: I can use getValue, but it returns null – Phoenix Mar 29 '20 at 17:50
  • I think the problem is because I try to get this value in MyWidget.java not in intent service for this widget – Phoenix Mar 29 '20 at 18:17
  • Yeah, that `reference` was a copy/paste error. I fixed it now. Aside from that, there is no reason why this code would get `null` from the database though. You might want to log the `dataSnapshot.child("name").getValue(String.class)` and see if that is correct. – Frank van Puffelen Mar 29 '20 at 18:28