2017-07-26 52 views
0

我想查詢Firebase並使用查詢的DataSnapshot中的條件數據填充回收器適配器。我試圖在正確記錄我想要的數據的if語句中放入填充函數,但是回收器視圖只是返回我在搜索的節點(我開始的主要查詢)中的所有內容。關於如何填充適用於「if」語句的項目的任何建議?謝謝!Android:如何在Firebase RecyclerView中使用條件DataSnapshot值?

rootRef = FirebaseDatabase.getInstance().getReference(); 
    //below is the node i query 
    mAlbumQuery = rootRef.child(Constants.FIREBASE_CHILD_ALBUMS).orderByChild("genres"); 
    mAlbumQuery.addValueEventListener(new ValueEventListener() { 
     @Override 
     public void onDataChange(DataSnapshot dataSnapshot) { 
      for (DataSnapshot reco : dataSnapshot.getChildren()) { 
       if (reco.getValue().toString().contains(mRecommendation.getGenre())) { 
        //below returns the items i want 
        Log.d("is this correct", reco.getValue().toString()); 
        //below returns everything in the original query 
        //how to populate only items that match the above? 
        mAdapter = new FirebaseRecyclerAdapter<Album, AlbumsViewHolder>(
          Album.class, 
          R.layout.album_cards, 
          AlbumsViewHolder.class, 
          mAlbumQuery) { 
         @Override 
         public void populateViewHolder(AlbumsViewHolder holder, Album album, int position) { 
          holder.bindView(album.getImage(), album.getTitle()); 
          if (!album.getGenres().contains(mRecommendation.getGenre())) { 
           //added as a hypothetical... should i have something in here? 
          } 
         } 
        }; 
        mAlbumsRecycler.setAdapter(mAdapter); 
       } 
      } 
     } 

     @Override 
     public void onCancelled(DatabaseError databaseError) { 

     } 
    }); 
    return view; 
} 

回答

0

,如果你想提取u可以使用這個任何特定節點: -

String notific = String.valueOf(dataSnapshot.getValue()); 

int key=dataSnapshot.getKey(); 
String title=String.valueOf(dataSnapshot.child("title").getValue()); 
String content=String.valueOf(dataSnapshot.child("content").getValue()); 
+0

我如何在Recycler視圖中使用它? – DarkKnightSDS

1

好吧,如果你發送mAlbumQuery作爲參數去你FirebaseRecyclerAdapter,我相信,它需要它的大小數項目。

作爲一個選項(快速修復),可以創建新的集合,這循環中:

for (DataSnapshot reco : dataSnapshot.getChildren()) { 
} 

可以填補這一新的集合與需要的物品。
循環後,您可以創建新的適配器並將過濾後的集合傳遞給它。

這裏是我看到這一點:

@Override 
     public void onDataChange(DataSnapshot dataSnapshot) { 
      Collection<> myNewCollection = new Collection<>(); //HashMap, ArrayList - depends on what you are storing in Firebase 
      for (DataSnapshot reco : dataSnapshot.getChildren()) { 
       if (reco.getValue().toString().contains(mRecommendation.getGenre())) { 
        //below returns the items i want 
        Log.d("is this correct", reco.getValue().toString()); 
        //below returns everything in the original query 
        //how to populate only items that match the above? 
        myNewCollection.add(reco.getValue); 
       } 
      } 

      recyclerView.setAdapter(new MyRecyclerViewAdapter(myNewCollection, ...)); 
     } 

也請看一看Firebase docsthis SO question
有一些有趣的方法 - startAt,endAtequalTo,這可能會幫助你。不幸的是,我沒有找到方法contains,但上面的方法可能已經足夠。

+0

是的,我認爲我認識到mAlbumQuery需要改變......我只是想弄清楚應該取代它的位置,或者我也許以不同的方式處理這個問題。我不確定。我應該在哪裏創建您提到的新集合? – DarkKnightSDS

+0

查看更新的答案。 –

+0

謝謝,我今天將會處理這件事,並讓你知道它是如何發生的。包含在對象中不存在,這就是爲什麼我必須先對String進行處理。我正在搜索字符串屬性中的特定關鍵字,並試圖僅返回那些具有關鍵字的關鍵字。 – DarkKnightSDS

相關問題