如何使用Android在Firebase数据库中获取值? [英] How can I get the value in Firebase Database with android?

查看:195
本文介绍了如何使用Android在Firebase数据库中获取值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Firebase实时数据库是这样的:

My Firebase Realtime Database is like this :

{Singer :
    Billie Eilish :
        01 :
            songType : "type01"
            songName : "bad guy"
        02 :
            songType : "type02"
            songName : "bury a friend"
    Lauv :
        01 :
            songType : "type01"
            songName : "I Like Me Better"
        02 :
            songType : "type03"
            songName : "lonely"
    Anne Marie :
        01 :
            songType : "type02"
            songName : "2002"
        ...
        ...
        ...
}

如果我想获得所有与"type01"相关的歌曲,该怎么办?这是我的Adapter类,在MainActivity中的recyclerView中显示数据.

If I want to get all the song that "type01", what should I do? This is my Adapter class that show the data in recyclerView in MainActivity.

适配器类:

public class MyAdapter extends Recycler.Adapter {
    ...
    @Override
    public void onBindViewHolder(@Nonnull ViewHolder holder, int i) {
        ...
        Query query = FirebaseDatabase.getInstance().getReference().child("Singer");
        options = new FirebaseRecyclerOptions.Builder<ItemModel>().setQuery(query, new SnapshotParser<ItemModel>() {
            @NonNull
            @Override
            public ItemModel parseSnapshot(@NonNull DataSnapshot snapshot) {
                if (snapshot.child("songType").getValue().toString().equals("type01") {
                    String song = snapshot.child("songName").getValue().toString();
                    return new ItemModel(song);
                }
                return null;
            }
        }).build();
        recyclerAdapter = new AnotherAdapter(options);
        recyclerAdapter.startListening();
}

我认为这在查询中很奇怪.它只称孩子为歌手".我想给所有孩子打电话,并得到什么songType是"type01". AnotherAdapter只是扩展了FirebaseReyclcerAdapter,并在其AnotherViewHolder中设置了文本.

I think it is weird in the query. It called only the child "Singer". I want to call all the child and get what songType is "type01". AnotherAdapter is just extending FirebaseReyclcerAdapter and just set the text in its AnotherViewHolder.

推荐答案

为什么不随便获取仅包含 songType:type01 的数据?

why don't you just go with fetch data which contains only songType : type01 try this

DatabaseReference reference = FirebaseDatabase.getInstance().getReference();

Query query = reference.child("Singer").orderByChild("songType").equalTo("type01");
query.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        if (dataSnapshot.exists()) {
            for (DataSnapshot issue : dataSnapshot.getChildren()) {
                //set featched value to recyclerview 
            }
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});

这篇关于如何使用Android在Firebase数据库中获取值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆