2017-01-11 69 views
2

這仍然不清楚如何使用自定義查詢填充FirebaseRecyclerAdapter。篩選查詢以填充FirebaseRecyclerAdapter

  1. 定義參考。這裏:DatabaseReference ref = mDatabase.getReference().child("users");

  2. 將它發送到適配器initAdapter(ref);

  3. 定義查詢並設置偵聽器。這樣對嗎?

    mAdapter = new FirebaseRecyclerAdapter<User, UserHolder>(User.class, R.layout.user_row_layout, UserHolder.class, ref) { 
    
         @Override 
         protected void populateViewHolder(final UserHolder viewHolder, final User model, int position) { 
    
          Query query = ref.orderByChild("group").equalTo(uid); 
          query.addChildEventListener(new ChildEventListener() { 
           @Override 
           public void onChildAdded(DataSnapshot dataSnapshot, String s) { 
            viewHolder.setName(model.getName()); 
            viewHolder.setStatus(getStatus(model.getStatus())); 
           } 
    
           // ... 
          }; 
    

仍然顯示來自用戶的所有數據。只需要根據uid進行過濾。任何線索?

+1

與你建立你的適配器的方式,你似乎呈現出用戶的列表。爲什麼你在'populateViewHolder'中附加了另一個子監聽器? –

回答

2

對於那些誰可能試圖使事情過於複雜,因爲我,或者在晚上工作,這裏是解決方案:

mAdapter = new FirebaseRecyclerAdapter<User, UserHolder>(
       User.class, 
       R.layout.user_row_layout, 
       UserHolder.class, 
       ref.orderByChild("group").equalTo(uid) 
) { 
    @Override 
    protected void populateViewHolder(final UserHolder viewHolder, final User model, int position) { 

       viewHolder.setName(model.getName()); 
       viewHolder.setStatus(getStatus(model.getStatus())); 
      } 

      // ... 
     }; 

潮!

0

一個簡單的例子,我顯示出與所有用戶排名列表:

FirebaseRecyclerAdapter<UserData,RankListViewHolder> adapter = new FirebaseRecyclerAdapter<UserData, RankListViewHolder> 
       (
      UserData.class, 
      R.layout.rank_item, 
      RankListViewHolder.class, 
      userRef.child("users").getRef() //--Set here your query 
       ) 
     { 
      @Override 
protected void populateViewHolder(RankListViewHolder viewHolder, UserData model, int position) 
      { 
       viewHolder.tvPos.setText(""+position+"º"); 
       viewHolder.userId = model.getId(); 
      } 
     };