我是一名經驗豐富的MySQL用戶,但我是json和firebase的新手。我試圖做一個用戶可以輸入數據的活動,並將其保存到我的數據庫的Firebase中。我達到了我的目標,但這是問題所在。當我保存它(我想)我需要指定一個字符串的子元素。由於此子元素是靜態的,因此每次用戶輸入問題時都會覆蓋數據庫中現有的問題。我該如何解決這個問題,以及如何獲取數據?這是我的代碼的一部分。每次用戶在Firebase中按下按鈕時如何存儲數據(Android)
private FirebaseAuth firebaseAuth;
private DatabaseReference databaseReference;
firebaseAuth=FirebaseAuth.getInstance();
if(firebaseAuth.getCurrentUser()==null){
finish();
startActivity(new Intent(this,LoginActivity.class));
}
FirebaseUser user=firebaseAuth.getCurrentUser();
databaseReference= FirebaseDatabase.getInstance().getReference();
Info info=new Info(question,answer,a,b,c,d);
databaseReference.child("secondQuestion").setValue(info);
//info is a new object which takes 6 string as an input.
// As you can see, I declare a string inside .child and it overwrites the data stored in it
您可能希望將問題存儲在類似列表的結構中:https://firebase.google.com/docs/database/android/lists-of-data。使用'push'會爲您的問題生成一個唯一的ID,您可能需要將其保存到某處以供稍後檢索。 –