gpt4 book ai didi

java - 我的 Firebase 数据库中的数据被覆盖。如何修复它?

转载 作者:行者123 更新时间:2023-12-02 01:20:45 26 4
gpt4 key购买 nike

我正在将多个编辑文本中的数据添加到我的 Firebase 数据库中。它添加得很好,但是当我尝试再次添加它时,它不会创建新对象。它会覆盖前一个。

public class HomeActivity extends AppCompatActivity {
Button logout;
EditText etfinalyear,etcnic,etname,etpaygroup,etregion,etcurrentposition,etcurrentaddress,etphonenumber,etmobilenumber;
FirebaseAuth mfirebaseauth;
Button btnsave;
private FirebaseAuth.AuthStateListener mauthstatelistener;
DatabaseReference reff;
users user;
ImageView ntc;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
ntc = findViewById(R.id.ntc);
etfinalyear = findViewById(R.id.edittextfinyear);
etcnic = findViewById(R.id.edittextcnic);
etname = findViewById(R.id.edittextname);
etpaygroup = findViewById(R.id.edittextpaygroup);
etregion = findViewById(R.id.edittextregion);
etcurrentposition = findViewById(R.id.edittextcurrentposition);
etcurrentaddress = findViewById(R.id.edittextcurrentaddress);
etphonenumber = findViewById(R.id.edittextphonenumber);
etmobilenumber = findViewById(R.id.edittextmobilenumber);
btnsave = findViewById(R.id.save);
logout = findViewById(R.id.signout);
reff = FirebaseDatabase.getInstance().getReference().child("users");
logout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FirebaseAuth.getInstance().signOut();
Intent logout = new Intent(HomeActivity.this,LoginActivity.class);
startActivity(logout);


}
});
user = new users();
btnsave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Long phn = Long.parseLong(etphonenumber.getText().toString());
Long mob = Long.parseLong(etmobilenumber.getText().toString());
user.setCnic(etcnic.getText().toString());
user.setFinyear(etfinalyear.getText().toString());
user.setName(etname.getText().toString());
user.setPaygroup(etpaygroup.getText().toString());
user.setDirectorate(etregion.getText().toString());
user.setCurrentposition(etcurrentposition.getText().toString());
user.setResidentialaddress(etcurrentaddress.getText().toString());
user.setPhonenumber(phn);
user.setMobilenumber(mob);
reff.child("user1").setValue(user);
//reff.push().setValue(user);
Toast.makeText(HomeActivity.this, "Data Saved Successfully", Toast.LENGTH_SHORT).show();
}
});



}
}

最佳答案

每次您插入相同的引用时,这就是它被替换的原因
请确保您在子方法 .child("user1") 中传递的 String 应与 Firebase 中的完全相同。,试试这个 -

 user = new users();
btnsave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Long phn = Long.parseLong(etphonenumber.getText().toString());
Long mob = Long.parseLong(etmobilenumber.getText().toString());
user.setCnic(etcnic.getText().toString());
user.setFinyear(etfinalyear.getText().toString());
user.setName(etname.getText().toString());
user.setPaygroup(etpaygroup.getText().toString());
user.setDirectorate(etregion.getText().toString());
user.setCurrentposition(etcurrentposition.getText().toString());
user.setResidentialaddress(etcurrentaddress.getText().toString());
user.setPhonenumber(phn);
user.setMobilenumber(mob);
DatabaseReference newRef = reff.child("user1").push();
newRef.setValue(user);
//reff.push().setValue(user);
Toast.makeText(HomeActivity.this, "Data Saved Successfully", Toast.LENGTH_SHORT).show();
}
});

关于java - 我的 Firebase 数据库中的数据被覆盖。如何修复它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57755375/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com