0

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?

Hammad Nasir
  • 2,889
  • 7
  • 52
  • 133
  • Looks like a scoping issue. You set `pS`, `pV`, and `pW` to the retrieved records, but then use different variables in prepareData() rather than passing those in. Also, this isn't a complete, minimal repro. See [how to ask](http://stackoverflow.com/help/how-to-ask) and [creating an mcve](http://stackoverflow.com/help/mcve). – Kato Jan 19 '17 at 17:23
  • @Kato when I used different variables in prepareData()? Those are all the variables retrieved in the code above. – Hammad Nasir Jan 19 '17 at 17:52
  • Firstly try make `idOfGP` local (not public) and see if that solve your problem. If it doesn't probably when `prepareData()` called, `pS`, `pNames`, `pUrls` is already full of value that getting last child of `pNames` will only get one specific items (the last called) – koceeng Jan 20 '17 at 06:22
  • However, I recommend placing each data acquired from Firebase Database into public `HashMap` and mark it with each key – koceeng Jan 20 '17 at 06:23
  • @koceeng need some help here too: http://stackoverflow.com/q/41910271/6144372 – Hammad Nasir Jan 28 '17 at 15:24
  • your own answer there maybe working but the flow is too risky. and it didn't use proper variable and data management. Looks like you seek solution that "work" only. maybe i'll pass your offer and let another answer your linked question there – koceeng Jan 28 '17 at 21:51

1 Answers1

0

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.

Hammad Nasir
  • 2,889
  • 7
  • 52
  • 133