从Firebase中的嵌套查询填充回收者视图 [英] Populating Recycler View From Nested Queries in Firebase

查看:52
本文介绍了从Firebase中的嵌套查询填充回收者视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用嵌套查询填充回收者视图.第一个查询转到groups_list节点,并获取该节点中的数据和唯一键.然后,它使用密钥将节点分组,并在该密钥下获取数据.这两个查询的结果都需要在回收者"视图中进行更新.

I am trying to populate a recycler view using nested queries. The first query goes to groups_list node and takes data in the node and the unique key. Then it goes to groups node with the key and gets data under that key. The result of both the queries needs to be updated in recycler view.

简而言之,第一个查询获取一些数据和一个键,该键用于进行第二个查询.这两个查询的结果都需要在回收者"视图中进行更新.我为此使用了模型类和回收者视图适配器.

In short, the first query gets some data and a key, the key is used to make the second query. The result from both these queries need to be updated in the recycler view. I am using a model class and recycler view adapter for this.

但是我在下面遇到了一个错误.

But I am getting an error below.

我的片段如下:

// Firebase
    fbDatabaseRootNode = FirebaseDatabase.getInstance().getReference();
    fbDatabaseRefGroupList = fbDatabaseRootNode.child("groups_list").child(current_user_id);
    fbDatabaseRefGroups = fbDatabaseRootNode.child("groups");

    fbDatabaseRefGroupList.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

            // Array to Get Group List
            lGroupsList = new ArrayList<>();

            if (dataSnapshot.exists()) {

                // Clear Array to Get Group List
                lGroupsList.clear();

                for (DataSnapshot glSnapshot : dataSnapshot.getChildren()) {

                    // Use The Model To Format Array List and Pass It Into It
                    GroupsListModel g = glSnapshot.getValue(GroupsListModel.class);

                    // Array to Get Group List
                    lGroupsList.add(g);

                    String groupID = String.valueOf(glSnapshot.getKey());

                    fbDatabaseRefGroups.child(groupID).addValueEventListener(new ValueEventListener() {
                        @Override
                        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                            if (dataSnapshot.exists()) {

                                for (DataSnapshot gSnapshot : dataSnapshot.getChildren()) {

                                    // Use The Model To Format Array List and Pass It Into It
                                    GroupsListModel g = gSnapshot.getValue(GroupsListModel.class);

                                    // Array to Get Group List
                                    lGroupsList.add(g);

                                }

                            }

                        }

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

                        }
                    });

                }

                aGroupList = new GroupsListAdapter(getContext(), lGroupsList);
                rvGroupList.setAdapter(aGroupList);

            }

        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
            System.out.println("The read failed: " + databaseError.getCode());

        }
    });

我的Firebase数据库结构看起来像

And My Firebase Database Structure Looks Like

  "groups" : {
    "-LaPfENd0G4pHlejrcd6" : {
      "group_creation_date" : 1553078221782,
      "group_logo" : "0",
      "group_member_count" : "0",
      "group_name" : "dog lovers",
      "group_tagline" : "we love dogs..."
    },
    "-LaPhG0YHnF3FG0Czxom" : {
      "group_creation_date" : 1553078751686,
      "group_logo" : "0",
      "group_member_count" : "0",
      "group_name" : "hi",
      "group_tagline" : "hello"
    }
  },
  "groups_list" : {
    "F81wvGx9a7fXRrfVPQMhQtkM0wv2" : {
      "-LaPfENd0G4pHlejrcd6" : {
        "block_status" : "0",
        "hide_status" : "0",
        "notification_status" : "0",
        "pin_sequence" : "0",
        "report_status" : "0"
      },
      "-LaPhG0YHnF3FG0Czxom" : {
        "block_status" : "0",
        "hide_status" : "0",
        "notification_status" : "0",
        "pin_sequence" : "0",
        "report_status" : "0"
      }
    }
  },

模型类是

public class GroupsListModel {

    private String block_status;
    private String hide_status;
    private String notification_status;
    private String pin_sequence;
    private String report_status;

    private String group_name;
    private Long group_creation_date;
    private String group_logo;
    private String group_member_count;
    private String group_tagline;

    public GroupsListModel() {
    }

    public GroupsListModel(String block_status, String hide_status, String notification_status, String pin_sequence, String report_status, String group_name, Long group_creation_date, String group_logo, String group_member_count, String group_tagline) {
        this.block_status = block_status;
        this.hide_status = hide_status;
        this.notification_status = notification_status;
        this.pin_sequence = pin_sequence;
        this.report_status = report_status;
        this.group_name = group_name;
        this.group_creation_date = group_creation_date;
        this.group_logo = group_logo;
        this.group_member_count = group_member_count;
        this.group_tagline = group_tagline;
    }

    public String getBlock_status() {
        return block_status;
    }

    public void setBlock_status(String block_status) {
        this.block_status = block_status;
    }

    public String getHide_status() {
        return hide_status;
    }

    public void setHide_status(String hide_status) {
        this.hide_status = hide_status;
    }

    public String getNotification_status() {
        return notification_status;
    }

    public void setNotification_status(String notification_status) {
        this.notification_status = notification_status;
    }

    public String getPin_sequence() {
        return pin_sequence;
    }

    public void setPin_sequence(String pin_sequence) {
        this.pin_sequence = pin_sequence;
    }

    public String getReport_status() {
        return report_status;
    }

    public void setReport_status(String report_status) {
        this.report_status = report_status;
    }

    public String getGroup_name() {
        return group_name;
    }

    public void setGroup_name(String group_name) {
        this.group_name = group_name;
    }

    public Long getGroup_creation_date() {
        return group_creation_date;
    }

    public void setGroup_creation_date(Long group_creation_date) {
        this.group_creation_date = group_creation_date;
    }

    public String getGroup_logo() {
        return group_logo;
    }

    public void setGroup_logo(String group_logo) {
        this.group_logo = group_logo;
    }

    public String getGroup_member_count() {
        return group_member_count;
    }

    public void setGroup_member_count(String group_member_count) {
        this.group_member_count = group_member_count;
    }

    public String getGroup_tagline() {
        return group_tagline;
    }

    public void setGroup_tagline(String group_tagline) {
        this.group_tagline = group_tagline;
    }
}

错误是

Can't convert object of type java.lang.Long to type com.example.myproject

数据快照的日志如下...第一个...

The logs from datasnapshots are coming as follows... first one...

第二个日志...

可能的解决方案1(传递给Recycler查看问题,否则可能会起作用)

这似乎是按正确的顺序获取数据,现在只需将其传递到模型数组列表"中并设置适配器即可.

This seems to be getting the data in proper sequence now just have to pass it into the Model Array List and Set The Adapter

// Get The Data
fbDatabaseRefGroupList.addChildEventListener(new ChildEventListener() {
    @Override
    public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

        if (dataSnapshot.exists()) {

            final String groupID = dataSnapshot.getKey();

            final String blockStatus = (String) dataSnapshot.child("block_status").getValue();
            final String hideStatus = (String) dataSnapshot.child("hide_status").getValue();
            final String notificationStatus = (String) dataSnapshot.child("notification_status").getValue();
            final String pinSequence = (String) dataSnapshot.child("pin_sequence").getValue();
            final String reportStatus = (String) dataSnapshot.child("report_status").getValue();

            fbDatabaseRefGroups.child(groupID).addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                    String groupName = (String) dataSnapshot.child("group_name").getValue();
                    String groupTagLine = (String) dataSnapshot.child("group_name").getValue();
                    String groupMemberCount = (String) dataSnapshot.child("group_name").getValue();


                }

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

                }
            });

        }

    }

    @Override
    public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

    }

    @Override
    public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {

    }

    @Override
    public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

    }

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

    }
});

可能的解决方案2(列表合并是一个问题-否则会起作用)

  // Firebase
    fbDatabaseRootNode = FirebaseDatabase.getInstance().getReference();
    fbDatabaseRefGroupList = fbDatabaseRootNode.child("groups_list").child(current_user_id);
    fbDatabaseRefGroups = fbDatabaseRootNode.child("groups");

    // Array to Get Group List
    lGroupsListList = new ArrayList<>();
    lGroupsList = new ArrayList<>();
    lCombinedList = new ArrayList<>();

    // Clear Array to Get Group List
    lGroupsList.clear();
    // Clear Array to Get Group List
    lGroupsListList.clear();
    // Clear Array to Get Group List
    lCombinedList.clear();

    ValueEventListener valueEventListener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            for (DataSnapshot ds : dataSnapshot.getChildren()) {

                // Use The Model To Format Array List and Pass It Into It
                GroupsListModel g = ds.getValue(GroupsListModel.class);

                // Array to Get Group List
                lGroupsListList.add(g);

                final String key = ds.getKey();

                final String blockStatus = (String) ds.child("block_status").getValue();

                DatabaseReference keyRef = fbDatabaseRootNode.child("groups").child(key);

                ValueEventListener eventListener = new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {

                        // Use The Model To Format Array List and Pass It Into It
                        GroupsListModel g = dataSnapshot.getValue(GroupsListModel.class);

                        // Array to Get Group List
                        lGroupsList.add(g);

                        String groupName = (String) dataSnapshot.child("group_name").getValue();

                        Log.d(TAG, "groupdetails: " + key + "--" + groupName + "--" + blockStatus);

                    }

                    @Override
                    public void onCancelled(@NonNull DatabaseError databaseError) {
                        Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
                    }
                };

                keyRef.addListenerForSingleValueEvent(eventListener);

            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
            Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
        }
    };

    aGroupList = new GroupsListAdapter(getContext(), lGroupsList);
    rvGroupList.setAdapter(aGroupList);

    fbDatabaseRefGroupList.addListenerForSingleValueEvent(valueEventListener);

@Prateek Jain您的答案有误,请参见以下屏幕截图:

@Prateek Jain Your answer is giving error please see screenshot below:

有效的解决方案基于Prateek Jains的输入

Working Solution Based on Prateek Jains Inputs

public class GroupsListFragment extends Fragment {

    private static final String TAG = "GroupsListFragment";

    // Recycler View
    private RecyclerView rvGroupList;
    private GroupsListAdapter aGroupList;

    private List<GroupsListModel> lGroupsListList;
    private List<GroupsListModel> lGroupsList;
    private List<GroupsListModel> lCombinedList;

    // Firebase
    private FirebaseAuth mAuth;
    private DatabaseReference fbDatabaseRootNode;
    private DatabaseReference fbDatabaseRefGroupList;
    private DatabaseReference fbDatabaseRefGroups;
    private String current_user_id;

    private String groupID;
    private List<String> lgroupIDs;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_groups_list, container, false);

        mAuth = FirebaseAuth.getInstance();
        current_user_id = mAuth.getCurrentUser().getUid();

        // Init Recycler View
        rvGroupList = view.findViewById(R.id.f_groups_list_groups_list);
        rvGroupList.setHasFixedSize(true);
        rvGroupList.setLayoutManager(new LinearLayoutManager(getActivity()));

        // Firebase
        fbDatabaseRootNode = FirebaseDatabase.getInstance().getReference();
        fbDatabaseRefGroupList = fbDatabaseRootNode.child("groups_list").child(current_user_id);
        fbDatabaseRefGroups = fbDatabaseRootNode.child("groups");

        // Get The Data
        fbDatabaseRefGroupList.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

                // Array to Get Group List
                lGroupsList = new ArrayList<>();

                if (dataSnapshot.exists()) {

                    // Clear Array to Get Group List
                    lGroupsList.clear();

                    final String groupID = dataSnapshot.getKey();

                    final String blockStatus = (String) dataSnapshot.child("block_status").getValue();
                    final String hideStatus = (String) dataSnapshot.child("hide_status").getValue();
                    final String notificationStatus = (String) dataSnapshot.child("notification_status").getValue();
                    final String pinSequence = (String) dataSnapshot.child("pin_sequence").getValue();
                    final String reportStatus = (String) dataSnapshot.child("report_status").getValue();

                    fbDatabaseRefGroups.child(groupID).addListenerForSingleValueEvent(new ValueEventListener() {
                        @Override
                        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                            Long groupCreationDate = (Long) dataSnapshot.child("group_creation_date").getValue();
                            String groupLogo = (String) dataSnapshot.child("group_logo").getValue();
                            String groupMemberCount = (String) dataSnapshot.child("group_member_count").getValue();
                            String groupName = (String) dataSnapshot.child("group_name").getValue();
                            String groupTagLine = (String) dataSnapshot.child("group_tagline").getValue();

                            lGroupsList.add(new GroupsListModel(blockStatus, hideStatus, notificationStatus, pinSequence,
                                    reportStatus, groupName, groupCreationDate, groupLogo, groupMemberCount, groupTagLine));

                            aGroupList.notifyDataSetChanged();
                        }

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

                        }
                    });

                    aGroupList = new GroupsListAdapter(getContext(), lGroupsList);
                    rvGroupList.setAdapter(aGroupList);

                }

            }

            @Override
            public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

            }

            @Override
            public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {

            }

            @Override
            public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

            }

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

            }
        });

        return view;

    }
}

推荐答案

您必须将所需的数据添加到适配器正在用来渲染视图的列表中.完成后,您必须调用 notifyDataSetChanged ,以便适配器可以从更新后的列表中重新加载其数据.

You have to add the required data to the list which is being used by your adapter to render the views. Once that is done you must call notifyDataSetChanged, so that adapter can reload its data from the updated list.

fbDatabaseRefGroups.child(groupID).addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

        String groupName = (String) dataSnapshot.child("group_name").getValue();
        String groupTagLine = (String) dataSnapshot.child("group_name").getValue();
        String groupMemberCount = (String) dataSnapshot.child("group_name").getValue();
        lGroupsList.add(new GroupsListModel(groupName, groupMemberCount, groupTagLine));
        aGroupList.notifyDataSetChanged();
        }

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

        }
   });

这篇关于从Firebase中的嵌套查询填充回收者视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆