myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String value = dataSnapshot.getValue(String.class);
Log.d(TAG, "Value is: " + value);
}
@Override
public void onCancelled(DatabaseError error) {
Log.w(TAG, "Failed to read value.", error.toException());
}
});
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("users");
String userid = "aloverlace"
String username = "Ada Lovelace"
myRef.child(userid).child("name").setValue(username)
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("posts");
String key = myRef.push().getKey();
HashMap<String, Object> postValues = new HashMap<>();
postValues.put("uid", "aloverlace");
postValues.put("author", "Ada Lovelace");
postValues.put("title", "hello post");
postValues.put("body", "hello body");
postValues.put("starCount", 0);
myRef.child(key).setValue(postValues);
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("posts/-Kz4JAW5cxfcezCgBjRi");
myRef.runTransaction(new Transaction.Handler() {
@Override
public Transaction.Result doTransaction(MutableData mutableData) {
Long starCount = mutableData.child("starCount").getValue(Long.class);
starCount++;
mutableData.child("starCount").setValue(starCount);
return Transaction.success(mutableData);
}
@Override
public void onComplete(DatabaseError databaseError, boolean b, DataSnapshot dataSnapshot) {
Log.d(TAG, "Transaction:onComplete:" + databaseError);
}
});
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference();
Query TopPostsQuery = databaseReference.child("posts").orderByChild("starCount");
TopPostsQuery.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) {
// TODO: handle the post
}
}
...
}
// Last 100 posts, these are automatically the 100 most recent
Query recentPostsQuery = databaseReference.child("posts").limitToFirst(100);