0
試圖發送一些數據火力卻深藏不露無法將數據發送到火力
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) {
}
}
可以顯示Patient類 –
@ svi.data我編輯,請 –