How I can get data of next child and show it in list view from firebase realtime database

爱⌒轻易说出口 提交于 2020-02-07 03:50:13

问题


I am developing an Audio Hadith, now I want to show the first key as a category like Ramadan, Zakat etc and in every category, there will lot of audio hadith's title and audio URL.

This is the database structure image:

public  void readHadiths(final DataStatus dataStatus){

        mDbReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

               hadiths.clear();

               List<String> keys = new ArrayList<>();

               for (DataSnapshot keyNode : dataSnapshot.getChildren()){

                        keys.add(keyNode.getKey());
                        String title = keyNode.child("title").getValue(String.class);
                        Hadith Hadits = keyNode.getValue(Hadith.class);
                        hadiths.add(Hadits);
                   Log.d("TAG", title);
               }
                dataStatus.DataLoaded(hadiths,keys);
            }

回答1:


You cannot query your actual database structure for getting all Hadith objects in one go because each one of those object exists in a separate node. To solve this, I recommend you change your database strucuture by reducing the number of nodes like this:

Firebase-root
    |
    --- Hadiths
          |
          --- hadith1
          |     |
          |     --- title: "This is hadith1 title"
          |     |
          |     --- url: "eg.example.com"
          |     |
          |     --- type: "Ramadan"
          |
          --- hadith2
                |
                --- title: "This is hadith2 title"
                |
                --- url: "eg.example.com"
                |
                --- type: "Zakat"

Now, to query all hadiths for Ramadan, please use the following query:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
Query query = rootRef.child("Hadiths").orderByChild("type").equalTo("Ramadan");
query.addListenerForSingleValueEvent(/* ... */);


来源:https://stackoverflow.com/questions/57075124/how-i-can-get-data-of-next-child-and-show-it-in-list-view-from-firebase-realtime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!