如何编写代码以比较Firebase中的数据?在我的Firebase下的医疗是“糖尿病”,当用户想要购买药品时,如何让数据库自动比较用户信息,如果用户患有糖尿病,将不允许用户购买药品?如何比较数据库数据?
https://i.stack.imgur.com/vxe2q.png
DatabaseReference myRef = FirebaseDatabase.getInstance().getReference();
FirebaseAuth auth = FirebaseAuth.getInstance();
FirebaseUser mUser = auth.getCurrentUser();
userID = mUser.getUid();
btnSubmit.setOnClickListener(this);
btnCancel.setOnClickListener(this);
}
private void submit(){
myRef.child("Users");
myRef.child(userID);
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
UserInformation user = dataSnapshot.getValue(UserInformation.class);
if(user!=null) {
if(user.getMedical().equals("Diabetes"))
Toast.makeText(getApplicationContext(),"Diabetes",Toast.LENGTH_LONG).show();
else
Toast.makeText(getApplicationContext(),"Not Not Diabetes",Toast.LENGTH_LONG).show();
myRef.child("Medicines").child("Pain and Fever").child(userID).setValue("Acetaminophen");
startActivity(new Intent(getApplicationContext(),MainActivity.class));
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
Userinformation
public UserInformation(String name, String address, String phone_num,String gender,String country,String medicine,String medical) {
this.address = address;
this.name = name;
this.phone_num = phone_num;
this.gender = gender;
this.country = country;
this.medicine = medicine;
this.medical = medical;
}
public String getMedical(){return medical;}
public void setMedical(String medical){this.medical = medical;}
你能告诉我你的UserInformation类 – Salman500
告诉我线56和87 – Salman500
你需要确保你的火力点的数据结构,每个用户都在例如你UserInformation类 – Salman500