digital_pro
digital_pro

Reputation: 183

How to get value of child key in firebase android?

I have below database structure in firebase. I am trying to get the key value "Std IX" inside onBindViewHolder and set it in class_key. I am able to get the key value "science" using below code in post_key field but, not able to get it's child key "Std IX" in class_key using String class_key = getRef(position).child(post_key).getKey();

enter image description here

   Query query = FirebaseDatabase.getInstance()
                .getReference()
                .child(user_id).child("List_of_subjects");

FirebaseRecyclerOptions<Subject_list_GetSet> options =
                    new FirebaseRecyclerOptions.Builder<Subject_list_GetSet>()
                            .setQuery(query, Subject_list_GetSet.class)
                            .build();    

adapter = new FirebaseRecyclerAdapter<Subject_list_GetSet, Subject_list_viewHolder>(options) {

                @Override
                protected void onBindViewHolder(@NonNull final Subject_list_viewHolder holder, int position, @NonNull Subject_list_GetSet model) {
                    final String post_key = getRef(position).getKey();

                    String class_key = getRef(position).child(post_key).getKey();


                    holder.setSubject_name(post_key);
                    holder.setClass(class_key);
                    holder.setBk1(model.getBk1());
                    holder.setBk2(model.getBk2());
                    holder.setBk3(model.getBk3());

Subject_list_GetSet.java

public class Subject_list_GetSet {
    private String Subject_name,Recom_bk,bk1,bk2,bk3;


    public Subject_list_GetSet(){}

    public Subject_list_GetSet(String Subject_name,String bk1,String bk2,String bk3){
        this.Subject_name=Subject_name;
       
        this.bk1=bk1;
        this.bk2=bk2;
        this.bk3=bk3;
    }

    public String getSubject_name() {
        return Subject_name;
    }

    public void setSubject_name(String Subject_name) {
        this.Subject_name = Subject_name;
    }


    public String getBk1() {
        return bk1;
    }

    public void setBk1(String bk1) {
        this.bk1 = bk1;
    }

    public String getBk2() {
        return bk2;
    }

    public void setBk2(String bk2) {
        this.bk2 = bk2;
    }

    public String getBk3() {
        return bk3;
    }

    public void setBk3(String bk3) {
        this.bk3 = bk3;
    }

}

Upvotes: 0

Views: 85

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 600006

Since you create an adapter on List_of_subjects, the adapter will try to show the direct child nodes under that level in the JSON. So from the screenshot, Firebase will try to create a Subject_list_GetSet for the science node, mapping the properties directly under that in the JSON to those in your Java class.

To match the JSON structure you'd need a field/property like this in the class:

@PropertyName("Std IX")
public String stdIX;

Since I expect that this key may be dynamically generated, this may not be possible. In that case the only way to get the right data in your adapter is to use a custom SnapshotParser as shown in the documentation.

Upvotes: 1

Related Questions