androidfirebaseandroid-recyclerviewfirebase-realtime-databasefastadapter

Unable to show all sets of data in RecyclerView retrieved from Firebase from under the keys which are retrieved before


In my code, I'm retrieving few keys stored under a reference and then using these keys I'm retrieving the data which are stored under these keys in the database.

Here's my code:

mDatabase.child("child").child(uid).addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {

            if (dataSnapshot.getValue() != null) {

                idOfGP = dataSnapshot.getValue().toString();

                mDatabase.child("anotherChild").child(idOfGP).addChildEventListener(new ChildEventListener() {
                    @Override
                    public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                        Map<String, String> mapData = (Map<String, String>) dataSnapshot.getValue();

                        pNames.add(mapData.get("pName"));

                        pUrls.add(mapData.get("pUrl"));

                        mDatabase.child("yetAnotherChild").child(idOfGP).addListenerForSingleValueEvent(new ValueEventListener() {
                            @Override
                            public void onDataChange(DataSnapshot dataSnapshot) {
                                Map<String, String> mapData = (Map<String, String>) dataSnapshot.getValue();
                                pS = mapData.get("s").trim();
                                pV = mapData.get("v").trim();
                                pW = mapData.get("w").trim();

                                prepareData();

                            }

                            @Override
                            public void onCancelled(DatabaseError databaseError) {

                            }
                        });


                    }
                    ...
                    ...

            } else {
                Toast.makeText(getBaseContext(), "No data here", Toast.LENGTH_SHORT).show();
            }

        }
        ...
        ...
    });

Here's prepareData():

public void prepareData() {
        rModelClass = new RModelClass(pS, pNames.get(pNames.size()-1), pUrls.get(pUrls.size()-1), pV, pW);
        fastItemAdapter.add(0, rModelClass);
        rRV.setAdapter(fastItemAdapter);
        rRV.smoothScrollToPosition(0);
    }

Here's RModelClass.java:

public class RModelClass extends AbstractItem<RModelClass, RModelClass.ViewHolder> {

    String pS, pWith, pV, pW, pUrl;

    public RModelClass() {}

    public RModelClass(String pS, String pWith, String pUrl, String pV, String pW)  {
        this.pS = pS.trim() + " with";
        this.pWith = pWith.trim();
        this.purl = pUrl;
        this.pV = pV.trim();
        this.pW = pW.trim();
    }

    @Override
    public int getType() {
        return R.id.recycler_view_r_p;
    }

    @Override
    public int getLayoutRes() {
        return R.layout.r_p_layout;
    }

    @Override
    public void bindView(RModelClass.ViewHolder holder, List payloads) {
        super.bindView(holder, payloads);

        holder.pS.setText(pS);
        holder.pWith.setText(pWith);
        holder.pV.setText(pV);
        holder.pW.setText(pW);
        holder.pUrl.setText(pUrl);

    }

    protected static class ViewHolder extends RecyclerView.ViewHolder {

        TextView pS, pWith, pV, pW, pUrl;

        public ViewHolder(View itemView) {
            super(itemView);

            pS = (TextView) itemView.findViewById(R.id.p_s);
            pWith = (TextView) itemView.findViewById(R.id.p_with);
            pV = (TextView) itemView.findViewById(R.id.p_v);
            pW = (TextView) itemView.findViewById(R.id.p_w);
            pUrl = (TextView) itemView.findViewById(R.id.p_url);

        }

    }

}

The reference: mDatabase.child("child").child(uid)... has 3 different keys stored in it, but what is happening is instead of showing 3 different sets of data in the recyclerview rRV, 3 same sets of data are getting shown.

What am I doing wrong and what should I do to show 3 different sets of data based on the 3 different keys retrieved from that reference?


Solution

  • Replacing the above given code with the code below did the job for me:

        mDatabase.child("child").child(uid).addChildEventListener(new ChildEventListener() {
                @Override
                public void onChildAdded(DataSnapshot dataSnapshot, String s) {
    
                    if (dataSnapshot.getValue() != null) {
    
                        idOfGP = dataSnapshot.getValue().toString();
                            mDatabase.child("anotherChild").child(idOfGP).addChildEventListener(new ChildEventListener() {
                                @Override
                                public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                                    Map<String, String> mapData = (Map<String, String>) dataSnapshot.getValue();
    
                                    pNames = mapData.get("pName").trim();
    
                                    pUrls = mapData.get("pUrl");
    
                                }
                                ...
                                ...
                            });
    
                            mDatabase.child("yetAnotherChild").child(idOfGP).addListenerForSingleValueEvent(new ValueEventListener() {
                                @Override
                                public void onDataChange(DataSnapshot dataSnapshot) {
                                    Map<String, String> mapData = (Map<String, String>) dataSnapshot.getValue();
                                    pS = mapData.get("s").trim();
                                    pV = mapData.get("v").trim();
                                    pW = mapData.get("w").trim();
    
                                    prepareData();
    
                                }
    
                                @Override
                                public void onCancelled(DatabaseError databaseError) {
    
                                }
                            });
    
                    } else {
                        Toast.makeText(getBaseContext(), "No recently played sport yet!", Toast.LENGTH_SHORT).show();
                    }
    
                }
                ...
                ...
            });
    

    Here's updated prepareData():

    public void prepareData() {
            rModelClass = new RModelClass(pS, pNames, pUrls, pV, pW);
            fastItemAdapter.add(0, rModelClass);
            rRV.setAdapter(fastItemAdapter);
            rRV.smoothScrollToPosition(0);
        }
    

    RModelClass.java remains same.