'How to get child of child from firebase database and add to recyclerview?

I am new to using firebase and android development. I am trying to get the children of children from my firebase database to a recyclerview. But so far, none of the menthods I have tried have worked.

Here is my JSON firebase database -

"ad": {
    "2020-06-06 15:28:36_01745654879": {
        "heading": "Require two room-mates at close proximity to BUBT",
        "user": {
            "user_id":"01745654879",
            "user_name": "Rofik Uddin"
        },
        "address_id": "A-2020-06-06 15:28:36_01745654879",
        "location": {
            "loc_name": "Rupnagar",
            "seats": 2
        },
        "gender_pref": "Male",
        "rent": {
            "rent_id":"R-2020-06-06 15:28:36_01745654879",
            "rent": 20000
        },
        "description": "Very close to BUBT.",
        "post_date": "2020-06-06",
        "rented": 0,
        "approved_by": "admin01"
    },

    "2021-06-08 21:15:09_01965487899": {
        "heading": "Spacious rooms with free WiFi",
        "user": {
            "user_id":"01965487899",
            "user_name": "Sofik Chowdhury"
        },
        "address_id": "A-2021-06-08 21:15:09_01965487899",
        "location": {
            "loc_name": "Rayer bazar",
            "seats": 3
        },
        "gender_pref": "Male",
        "rent": {
            "rent_id":"R-2021-06-08 21:15:09_01965487899",
            "rent": 10000
        },
        "description": "",
        "post_date": "2021-06-08",
        "rented": 0,
        "approved_by": "admin03"
    },

    "2021-02-09 17:45:22_01965487896": {
        "heading": "Room-mate required with preference to BUBT students",
        "user": {
            "user_id":"01965487896",
            "user_name": "Mitu Sultana"
        },
        "address_id": "A-2021-02-09 17:45:22_01965487896",
        "location": {
            "loc_name": "Mirpur - 2",
            "seats": 1
        },
        "gender_pref": "Female",
        "rent": {
            "rent_id":"R-2021-02-09 17:45:22_01965487896",
            "rent": 17300
        },
        "description": "Next to Mirpur-2 mosque market.",
        "post_date": "2021-02-09",
        "rented": 0,
        "approved_by": "admin02"
    }
}

Here is my java class -

public class Ad {

String heading, post_date, address_id, approved_by, description, gender_pref;
int rented;

public String getHeading() {
    return heading;
}

public String getPost_date() {
    return post_date;
}

public String getAddress_id() {
    return address_id;
}

public String getApproved_by() {
    return approved_by;
}

public String getDescription() {
    return description;
}

public String getGender_pref() {
    return gender_pref;
}

public String getRented() {
    return String.valueOf(rented);
}

}

Here is my Adapter -

public class AdAdapter extends RecyclerView.Adapter<AdAdapter.AdViewHolder> {

Context context;
ArrayList<Ad> ad_detailsList;

public AdAdapter(Context context, ArrayList<Ad> ad_holderList) {
    this.context = context;
    this.ad_detailsList = ad_holderList;
}

@NonNull
@Override
public AdViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(context).inflate(R.layout.activity_ad_holder, parent, false);
    return new AdViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull AdViewHolder holder, int position) {
    Ad ad = ad_detailsList.get(position);

    holder.title.setText(ad.getHeading());

    SimpleDateFormat old_format = new SimpleDateFormat("yyyy-mm-dd");
    Date new_format = null;
    try {
        new_format = old_format.parse(ad.getPost_date());
        old_format.applyPattern("dd MMMM yyyy");
    } catch (ParseException e) {
        e.printStackTrace();
    }

    holder.post_date.setText(old_format.format(new_format));
}

@Override
public int getItemCount() {
    return ad_detailsList.size();
}

public static class AdViewHolder extends RecyclerView.ViewHolder {

    TextView title, ad_publisher, seat, rent, post_date;

    public AdViewHolder(@NonNull View itemView) {
        super(itemView);

        title = itemView.findViewById(R.id.title_result);
        ad_publisher = itemView.findViewById(R.id.ad_publisher_result);
        seat = itemView.findViewById(R.id.seat_result);
        rent = itemView.findViewById(R.id.rent_result);
        post_date = itemView.findViewById(R.id.post_date_result);
    }
}

}

And this is how I have implemented it as of now -

FirebaseDatabase database = FirebaseDatabase.getInstance();
    myRef = database.getReference("ad");
    Query query = myRef.orderByChild("post_date").limitToLast(10);

    adList = new ArrayList<>();
    adAdapter = new AdAdapter(this, adList);

    query.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {
            for (DataSnapshot dataSnapshot: snapshot.getChildren()) {
                Ad ad = new Ad();

                ad = dataSnapshot.getValue(Ad.class);
                adList.add(ad);

                adHolder.setAdapter(adAdapter);
            }

            adAdapter.notifyDataSetChanged();
        }

        @Override
        public void onCancelled(@NonNull DatabaseError error) {

        }
    });

I wanna add the user_name and rent values from the database to the recyclerview. I have tried to use nested listeners. But for some reason, the inner listener produces output after the outer listener has already been executed.

I would really appreciate it if someone could help me with this.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source