2017-02-15 63 views
0

我試圖從firebase數據庫檢索一些數據並將其填充到recyclerview。查詢背後的邏輯是顯示異性用戶作爲當前用戶,但同一個大廳。例如,如果當前用戶是「男孩」並且在「Legon」大廳中,則從數據庫填充legon大廳中的「女性」的回收站視圖。如何使多個查詢在Firebase中填充recyclerview?

用戶數據庫:

這就是我想要做的,但不工作

@Override 
protected void onStart() { 
     super.onStart(); 

    final String user_id = firebaseAuth.getCurrentUser().getUid(); 
    final User user = new User(); 

    final String halls = user.getHalls(); 


    databaseReference.addValueEventListener(new ValueEventListener() { 
     @Override 
     public void onDataChange(DataSnapshot dataSnapshot) 

     { 

      if(dataSnapshot.child("Male").hasChild(user_id)){ 


       query = databaseReference.child("Female").orderByChild("halls").equalTo(halls); 
      } 
      else{ 

       query = databaseReference.child("Male").orderByChild("halls").equalTo(halls); 
      } 
     } 
     @Override 
     public void onCancelled(DatabaseError databaseError){ 

     } 
    }); 

     FirebaseRecyclerAdapter<User, UserViewholder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<User, UserViewholder>(
     User.class, 
     R.layout.users_row, 
     UserViewholder.class, 
     query 

     ) { 
@Override 
protected void populateViewHolder(UserViewholder viewHolder, User model, int position) { 
     viewHolder.setUsername(model.getUsername()); 
     viewHolder.setLevels(model.getLevels()); 
     viewHolder.setDownloadUrl(getApplicationContext(), model.getDownloadUrl()); 
     } 

     }; 

     users2.setAdapter(firebaseRecyclerAdapter); 

     } 

回答

0

我覺得你在這裏是錯誤的

final User user = new User(); 
final String halls = user.getHalls(); 

因爲halls總回報null

你應該改變它爲

User user = firebaseAuth.getCurrentUser(); 
final String halls = user.getHalls(); 
+0

請「用戶」只是我創建的類包含一些信息沒有..它不是「firebaseUser」@Phan Van Linh –