0

在我的代码中,我检索了几个keys存储在引用下的数据,然后使用这些键检索存储在keys数据库中的数据。

这是我的代码:

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

        }
        ...
        ...
    });

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

这是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);

        }

    }

}

参考:其中存储了mDatabase.child("child").child(uid)...3 个不同keys的数据,但正在发生的不是在 recyclerview 中显示 3 个不同的数据集,而是显示了 3rRV个相同的数据集。

我做错了什么,我应该怎么做才能根据从该引用中检索到的 3 个不同的键来显示 3 个不同的数据集?

4

1 回答 1

0

用下面的代码替换上面给定的代码为我完成了这项工作:

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

            }
            ...
            ...
        });

这里更新了prepareData()

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

RModelClass.java保持不变。

于 2017-01-20T12:17:42.780 回答