2017-07-26 45 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

相关问题