Reputation: 21
I followed a Youtube video on accessing a specific value from my database, this is exactly what the video had (except I changed it to my values). When their app ran, it made a toast with the corresponding value(s). When I start mine I get the error "Unfortunately, App has stopped." Am I doing something wrong or am I going about this the wrong way?
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FirebaseDatabase database = FirebaseDatabase.getInstance();
test = database.getReference("https://matts-macros.firebaseio.com/values");
test.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Map<String, String> data = (Map<String, String>) dataSnapshot.getValue();
Toast.makeText(getApplicationContext(), "Value: + " + data.get("adjustedcarbs"), Toast.LENGTH_LONG).show();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
Upvotes: 1
Views: 51
Reputation: 11995
You're accessing the reference in a wrong way. See the docs. You should do one of two things:
1) Replace this:
test = database.getReference("https://matts-macros.firebaseio.com/values");
with this:
test = database.getReference().child("values");
2) If you want to use your url, you should do this instead:
String myUrl = "https://matts-macros.firebaseio.com/values";
test = FirebaseDatabase.getInstance().getReferenceFromUrl(myUrl);
Regarding your parsing error, try with a Map<String,Object>
:
Map<String, Object> data = (Map<String, Object>) dataSnapshot.getValue();
Upvotes: 2