android– 从Firebase中的嵌套查询填充Recycler视图

android– 从Firebase中的嵌套查询填充Recycler视图,第1张

概述我正在尝试使用嵌套查询填充回收站视图.第一个查询转到groups_list节点,并获取节点中的数据和唯一键.然后它使用密钥进入组节点并获取该密钥下的数据.两个查询的结果都需要在recycleler视图中更新.简而言之,第一个查询获取一些数据和一个键,该键用于进行第二次查询.需要在回收器视

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

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

但我在下面收到错误.

我的片段如下:

// 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数据库结构看起来像

  "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

来自数据快照的日志如下……首先……


来自第二个的日志……

可能的解决方案1(传递给回收站查看其他工作的问题)

这似乎是以正确的顺序获取数据,现在只需将其传递到模型阵列列表并设置适配器

// Get The DatafbDatabaseRefGroupList.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 Jains输入的工作解决方案

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,以便适配器可以从更新的列表重新加载其数据.

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) {        }   });
总结

以上是内存溢出为你收集整理的android – 从Firebase中的嵌套查询填充Recycler视图全部内容,希望文章能够帮你解决android – 从Firebase中的嵌套查询填充Recycler视图所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1096724.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存