如何将文件共享首选项值保存到firebase并检索它

时间:2017-12-22 16:07:10

标签: firebase-realtime-database synchronization sharedpreferences

我正在做一个简单的游戏应用程序 其中两个简单游戏的总分存储在文件共享首选项

如何将分数与firebase同步 并检索总分活动中的分数

请帮忙

1 个答案:

答案 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());
    }
});