firebase lists code example
Example 1: display arraylist messages realtime database
dbref.addValueEventListener(new com.google.firebase.database.ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
ArrayList<String> list = new ArrayList<>();
list.clear();
for(DataSnapshot ds :dataSnapshot.getChildren()) {
Score Result = ds.getValue(Score.class);
String userId = String.valueOf(Result.getUserId());
String score = String.valueOf(Result.getScore());
list.add(userId);
list.add(score);
}
adapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, list);
LvRanking.setAdapter(adapter);
}
Example 2: how to push multiple data to different parents in a single request in firebase
var ref = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com");
var newPostRef = ref.child("posts").push();
var newPostKey = newPostRef.key();
var updatedUserData = {};
updatedUserData["user/posts/" + newPostKey] = true;
updatedUserData["posts/" + newPostKey] = {
title: "New Post",
content: "Here is my new post!"
};
ref.update(updatedUserData, function(error) {
if (error) {
console.log("Error updating data:", error);
}
});