2017-06-14 68 views
0

我想读取Firebase中的数据,它是由用户键入的医疗=“糖尿病”。如果此用户有糖尿病病史,将显示不允许用户购买的东西。任何人都可以教我如何在android studio中编写这个条件?如何比较Firebase中的数据?

Firebase data structure

public class Pain_and_Fever extends AppCompatActivity implements View.OnClickListener{ 

private Button btnSubmit, btnCancel; 
private String userID; 
Query query; 


//add Firebase Database stuff 
private FirebaseDatabase mFirebaseDatabase; 
private FirebaseAuth mAuth; 
private FirebaseAuth.AuthStateListener mAuthListener; 
private DatabaseReference myRef; 

@Override 
protected void onCreate(@Nullable final Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_pain_and__fever); 
    btnSubmit = (Button) findViewById(R.id.bttnsubmit); 
    btnCancel = (Button) findViewById(R.id.bttncancel); 


    //declare the database reference object. This is what we use to access the database. 
    //NOTE: Unless you are signed in, this will not be useable. 
    mAuth = FirebaseAuth.getInstance(); 
    mFirebaseDatabase = FirebaseDatabase.getInstance(); 
    final FirebaseUser user = mAuth.getCurrentUser(); 
    userID = user.getUid(); 
    myRef = mFirebaseDatabase.getReference(); 

    btnSubmit.setOnClickListener(this); 
    btnCancel.setOnClickListener(this); 

    query = myRef.orderByChild("medical").equalTo("Diabetes"); 

} 

    private void submit(){ 
     query.addListenerForSingleValueEvent(new ValueEventListener() { 
      @Override 
      public void onDataChange(DataSnapshot dataSnapshot) { 
       if(dataSnapshot.exists()){ 

        for (DataSnapshot issue : dataSnapshot.getChildren()){ 
         UserInformation uInfo = issue.getValue(UserInformation.class); 
         if (uInfo.getMedical().equals("Diabetes")){ 
          startActivity(new Intent(getApplicationContext(),Medicine.class)); 

         }else{ 
          myRef.child("Medicines").child("Pain and Fever").child(userID).setValue("Acetaminophen"); 
          startActivity(new Intent(getApplicationContext(),Medicine.class)); 
         } 
        } 
       } 
      } 

      @Override 
      public void onCancelled(DatabaseError databaseError) { 

      } 
     }); 

     } 


    @Override 
    public void onClick(View view) { 
     if (view == btnSubmit){ 
      submit(); 
     } 
     if (view == btnCancel){ 
      startActivity(new Intent(this,Medicine.class)); 
     } 
    } 


} 
+0

使用查询为 –

+0

查询查询= reference.child( “用户”)orderByChild( “ID”)。使用这个? – Bleach

+0

看到我的答案,并根据您的要求更改它 –

回答

1

尝试这种方式,这对我的作品

Query chatRoomsQuery = mFirebaseDatabase.orderByChild("medical").equalTo("your value"); 


         chatRoomsQuery.addListenerForSingleValueEvent(new ValueEventListener() { 
          @Override 
          public void onDataChange(DataSnapshot dataSnapshot) { 
           if (dataSnapshot.exists()) { 
            // dataSnapshot is the "issue" node with all children with id 0 

            search_list=new ArrayList<SearchModel>(); 
            for (DataSnapshot issue : dataSnapshot.getChildren()) { 
             // do something with the individual "issues" 
             UserRegisterModel mModel = issue.getValue(UserRegisterModel.class); 
             if(mModel.getArea().equals(sel_area)) 

            hidepDialog(); 

           } 
          } 

          @Override 
          public void onCancelled(DatabaseError databaseError) { 

          } 
         }); 

        }