我是一名经验丰富的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,您可能需要将其保存到某处以供稍后检索。 –