尝试将一些数据发送到firebase但没有显示任何内容
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
private TextView txtDetails;
private EditText name,age,gender,mobile,address,blood,bp,date;
private Button submit;
private FirebaseDatabase firebaseInstance;
private DatabaseReference firebaseDatabase;
private String patientId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtDetails = findViewById(R.id.txtheading);
name = findViewById(R.id.edname);
age = findViewById(R.id.edage);
gender = findViewById(R.id.edgender);
mobile = findViewById(R.id.edmobile);
blood = findViewById(R.id.edblood);
bp = findViewById(R.id.edbp);
date = findViewById(R.id.eddate);
submit = findViewById(R.id.btnsubmit);
firebaseInstance = FirebaseDatabase.getInstance();
firebaseDatabase = firebaseInstance.getReference("Doctor");
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String Name = name.getText().toString();
String Age = age.getText().toString();
String Gender = gender.getText().toString();
String Mobile = mobile.getText().toString();
String Blood = blood.getText().toString();
String BP = bp.getText().toString();
String Date = date.getText().toString();
if(TextUtils.isEmpty(patientId)) {
createPatient(Name,Age,Gender,Mobile,Blood,BP,Date);
}
else {
updatePatient(Name,Age,Gender,Mobile,Blood,BP,Date);
}
}
});
}
private void updatePatient(String name, String age, String gender,
String mobile, String blood,
String bp, String date) {
}
private void createPatient(String name, String age, String gender,
String mobile, String blood, String bp, String date) {
if(TextUtils.isEmpty(patientId)) {
patientId = firebaseDatabase.push().getKey();
}
Patient patient = new Patient(name,age,gender,mobile,blood,bp,date);
firebaseDatabase.child(patientId).setValue(patient);
addUserChangeListener();
}
private void addUserChangeListener() {
firebaseDatabase.child(patientId).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Patient patient = dataSnapshot.getValue(Patient.class);
if(patient==null) {
Log.e(TAG, "User data is changed !" );
return;
}
Log.e(TAG, "User data is changed!" + patient.Names + ", " + patient.Address +", "+patient.Age +
"," +patient.Blood +"," +patient.BP+ "," +patient.Gender +
"," +patient.Mobile + "," +patient.Date);
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.e(TAG, "Failed to read user", databaseError.toException());
}
});
}
}
这是我的Patient
班级:
public class Patient {
public String Names;
public String Age;
public String Gender;
public String Mobile;
public String Address;
public String Blood;
public String BP;
public String Date;
public Patient(String name, String age, String gender, String mobile, String blood, String bp, String date) {
}
public Patient(String names, String age, String gender,
String mobile, String address, String blood,
String BP, String date) {
Names = names;
Age = age;
Gender = gender;
Mobile = mobile;
Address = address;
Blood = blood;
this.BP = BP;
Date = date;
}
public Patient(String name, String age, String gender, String mobile, String blood, String bp, String date) {
}
}
答案 0 :(得分:0)
首先删除它:
if(TextUtils.isEmpty(patientId)) {
}
来自:
createPatient(Name,Age,Gender,Mobile,Blood,BP,Date);
和此:
patientId = firebaseDatabase.push().getKey();
然后使用地图而不是类来存储信息。
所以删除这个
Patient patient = new Patient(name,age,gender,mobile,blood,bp,date);
和createPatient方法将如下所示
private void createPatient(String name, String age, String gender,
String mobile, String blood, String bp, String date) {
//create a push id like that
patientId = firebaseDatabase.push().getKey();
//make a map called patientmap
Map patientmap=new HashMap();
//add your values like this to the map
patientmap.put("Name",name);
patientmap.put("Age",age);
patientmap.put("Gender",gender);
patientmap.put("Mobile",mobile);
patientmap.put("Blood",blood);
patientmap.put("BP",bp);
patientmap.put("Date",date);
firebaseDatabase.child(patientId).setValue(patientmap);
addUserChangeListener();
}