【发布时间】:2019-08-01 18:01:07
【问题描述】:
[这是我的json数据This is my code][1]
我正在尝试更改 Firebase 数据库中键值对的值。 假设我有一个像这样的键值:“化学”:“20”。我想提取键化学的值,并想在添加一些数字后更新它,比如我加 10,所以我的最终键值对将变成 "chemistry":"30" 。但是我无法访问密钥,有人可以帮我解决这个问题。
我的代码放在这里:
mAuth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
Log.d("TAG", "signInWithEmail:success");
//extracting the current user from the databse
FirebaseUser user = mAuth.getCurrentUser();
//getting the uid of the user
String Uid=user.getUid();
//getting the databse instance for the particular user
FirebaseDatabase database = FirebaseDatabase.getInstance();
//setting the path to the Users/Uid to read the key values
DatabaseReference myRef = database.getReference("Users/"+Uid);
//textView.setText(myRef);
String key=myRef.child("chemistry").getKey();
textView.setText(key);
// myRef.setValue("Hello, World!");
} else {
// If sign in fails, display a message to the user.
Log.w("TAG", "signInWithEmail:failure", task.getException());
Toast.makeText(TestActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
//updateUI(null);
}
// ...
}
});
我的 json 在这里:
{
"Users" : {
"LMn5GeZixNQNpdQhGzdKNbTgIw53" : {
"chemistry" : "20",
"email" : "umairnsr87@gmail.com",
"lastScore" : "0",
"maths" : "0",
"name" : "umair",
"phone" : "123456789",
"physics" : "0"
}
}
}
【问题讨论】:
标签: java android firebase firebase-realtime-database