Android firebase实时数据库只显示最后一次提交



我一直在遵循如何在Android中保存数据到Firebase实时数据库的教程。

与教程,我已经设法得到这个数据保存等,但它只显示最后提交的项目。

MainActivity代码:

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
public class MainActivity extends AppCompatActivity {
// creating variables for
// EditText and buttons.
private EditText employeeNameEdt, employeePhoneEdt, employeeAddressEdt;
private Button sendDatabtn;
// creating a variable for our
// Firebase Database.
FirebaseDatabase firebaseDatabase;
// creating a variable for our Database
// Reference for Firebase.
DatabaseReference databaseReference;
// creating a variable for
// our object class
EmployeeInfo employeeInfo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// initializing our edittext and button
employeeNameEdt = findViewById(R.id.idEdtEmployeeName);
employeePhoneEdt = findViewById(R.id.idEdtEmployeePhoneNumber);
employeeAddressEdt = findViewById(R.id.idEdtEmployeeAddress);
// below line is used to get the
// instance of our FIrebase database.
firebaseDatabase = FirebaseDatabase.getInstance();
// below line is used to get reference for our database.
databaseReference = firebaseDatabase.getReference("EmployeeInfo");
// initializing our object
// class variable.
employeeInfo = new EmployeeInfo();
sendDatabtn = findViewById(R.id.idBtnSendData);
// adding on click listener for our button.
sendDatabtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// getting text from our edittext fields.
String name = employeeNameEdt.getText().toString();
String phone = employeePhoneEdt.getText().toString();
String address = employeeAddressEdt.getText().toString();
// below line is for checking weather the
// edittext fields are empty or not.
if (TextUtils.isEmpty(name) && TextUtils.isEmpty(phone) && TextUtils.isEmpty(address)) {
// if the text fields are empty
// then show the below message.
Toast.makeText(MainActivity.this, "Please add some data.", Toast.LENGTH_SHORT).show();
} else {
// else call the method to add
// data to our database.
addDatatoFirebase(name, phone, address);
}
}
});
}
private void addDatatoFirebase(String name, String phone, String address) {
// below 3 lines of code is used to set
// data in our object class.
employeeInfo.setEmployeeName(name);
employeeInfo.setEmployeeContactNumber(phone);
employeeInfo.setEmployeeAddress(address);
// we are use add value event listener method
// which is called with database reference.
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
// inside the method of on Data change we are setting
// our object class to our database reference.
// data base reference will sends data to firebase.
databaseReference.setValue(employeeInfo);
// after adding this data we are showing toast message.
Toast.makeText(MainActivity.this, "data added", Toast.LENGTH_SHORT).show();
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
// if the data is not added or it is cancelled then
// we are displaying a failure toast message.
Toast.makeText(MainActivity.this, "Fail to add data " + error, Toast.LENGTH_SHORT).show();
}
});
}
}

EmployeeInfo代码:

public class EmployeeInfo {
// string variable for
// storing employee name.
private String employeeName;
// string variable for storing
// employee contact number
private String employeeContactNumber;
// string variable for storing
// employee address.
private String employeeAddress;
// an empty constructor is
// required when using
// Firebase Realtime Database.
public EmployeeInfo() {
}
// created getter and setter methods
// for all our variables.
public String getEmployeeName() {
return employeeName;
}
public void setEmployeeName(String employeeName) {
this.employeeName = employeeName;
}
public String getEmployeeContactNumber() {
return employeeContactNumber;
}
public void setEmployeeContactNumber(String employeeContactNumber) {
this.employeeContactNumber = employeeContactNumber;
}
public String getEmployeeAddress() {
return employeeAddress;
}
public void setEmployeeAddress(String employeeAddress) {
this.employeeAddress = employeeAddress;
}
}

有人在Firebase中遇到过这种情况吗?我一直在寻找其他教程,最终就像这样,所以我不确定它是否在代码中有问题或与我的Firebase数据库。

因为firebase会覆盖您之前输入的数据。

您可以在setValue之前使用这样的.push()函数。

DatabaseReference database = FirebaseDatabase.getInstance().getReference("EmployeeInfo");

,

database.push().setValue(employeeObject);

.push()方法在数据库中创建一个具有唯一键的子节点。

尝试查看firebase文档以获取更多信息在Firebase上保存数据

键是这样的Firebase RTDB