我正在做一个简单的游戏应用程序 其中两个简单游戏的总分存储在文件共享首选项
中如何将分数与firebase同步 并检索总分活动中的分数
请帮忙
答案 0 :(得分:0)
Create a model class with User with properties name and email . create setters and getters and store the model object to firebase as below.
DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference("users");
String userId = mDatabase.push().getKey();
User user = new User("username", "email");
// pushing user to 'users' node using the userId
mDatabase.child(userId).setValue(user);
Retreive the data in other class using the below code
*******************************************************
mDatabase.child(userId).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
User user = dataSnapshot.getValue(User.class);
Log.d(TAG, "User name: " + user.getName() + ", email " + user.getEmail());
}
@Override
public void onCancelled(DatabaseError error) {
// Failed to read value
Log.w(TAG, "Failed to read value.", error.toException());
}
});