How can I get the value in Firebase Database with android?

本小妞迷上赌 提交于 2019-12-02 05:56:31
Black mamba

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) {

    }
});

Since you're passing a reference to Singers into the recycler view, your parseSnapshot is called for each singer. In there you need to make sure you check the songType of each song child of the singer, where now you check the songType of the singers themselves (which never exists, so never goes into the if block).

So it'd be something like this:

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) {
        String song = "No song found";
        for (DataSnapshot songSnapshot: snapshot.getChildren()) {
            String songType = songSnapshot.child("songType").getValue(String.class)
            if (songType.equals("type01") {
              song = songType;
              ...

I'm not sure though if this is what you want, because you seem to want a list of songs, while you're passing in a list of singers. The above will only work if each singer has exactly one song of type01 (not 0, not more).

If that is not the case, the adapters in FirebaseUI won't fit your needs with your current data structure, since they show a list of items from the database, while you want to show items from a tree-like structure.

The two main options that come to mind in that case:

  1. Build your own adapter, typically based on ArrayAdapter as shown here.
  2. Change your data structure so that all songs are in a flat list (and the singer then becomes a property of each song).
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!