简体   繁体   中英

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

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

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(/* ... */);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM