Android:如何在Firebase RecyclerView中使用条件DataSnapshot值?

时间:2017-07-26 07:14:12

标签: android firebase firebase-realtime-database firebaseui

我正在尝试查询Firebase并使用查询的DataSnapshot中的条件数据填充回收器适配器。我尝试将populate函数放在if语句中,正确记录我想要的数据,但是回收器视图只返回我正在搜索的节点(我开始的主查询)中的所有内容。有关如何填充适用于“if”语句的项目的任何建议?谢谢!

    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;
}

2 个答案:

答案 0 :(得分:1)

如果你想提取任何特定的节点你可以使用它: -

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());

答案 1 :(得分: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 有一些有趣的方法 - startAtendAtequalTo,可能会对您有所帮助。遗憾的是,我没有找到方法contains,但上述方法对您来说已经足够了。