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"
}
}
}
}
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?
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:
If you only need the name of the user, you can also attach your listener to just that property and save some bandwidth: