Ahmad Hayyat
Ahmad Hayyat

Reputation: 9

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:

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

Upvotes: 0

Views: 94

Answers (1)

Alex Mamo
Alex Mamo

Reputation: 138869

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

Upvotes: 1

Related Questions