代码不会解除Firebase数据库儿童事件侦听器错误



我正在使用Firebase作为后端服务器开发基于Android的应用程序。我以以下方式设计了应用程序的结构:

Project Name
|_ Products
|_ groups
   |_ 1BME
      |_ members
         |_ -K4usWDhtiw4U
             |_ Custom Object
         |_ -K4uscDHwYsXHs
             |_ Custom Object

自定义对象是:

package com.app.shubhamjhunjhunwala.heritagecompanion_students;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
/**
 * Created by shubham on 23/01/17.
 */
public class UserDetails {
    UserDetails() {}
    public String name;
    public String email;
    public String password;
    public String phone;
    public String roll;
    public String department;
    public String year;
    public String section;
    public String dpDownloadUri;
    public UserDetails(String name, String email, String password, String phone, String roll, String department, String year, String section, String dpDownloadUri) {
        this.name = name;
        this.email = email;
        this.password = password;
        this.phone = phone;
        this.roll = roll;
        this.department = department;
        this.year = year;
        this.section = section;
        this.dpDownloadUri = dpDownloadUri;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone;
    }
    public String getRoll() {
        return roll;
    }
    public void setRoll(String roll) {
        this.roll = roll;
    }
    public String getDepartment() {
        return department;
    }
    public void setDepartment(String department) {
        this.department = department;
    }
    public String getYear() {
        return year;
    }
    public void setYear(String year) {
        this.year = year;
    }
    public String getSection() {
        return section;
    }
    public void setSection(String section) {
        this.section = section;
    }
    public String getDpDownloadUri() {
        return dpDownloadUri;
    }
    public void setDpDownloadUri(String dpDownloadUri) {
        this.dpDownloadUri = dpDownloadUri;
    }
}

我的意图是将用户详细信息在1BME下的成员儿童下获取子女子女的详细信息。因此,我在程序中使用以下代码:

mDatabase = FirebaseDatabase.getInstance();
mGroupMembersDatabaseReference = mDatabase.getReference().child("groups").child(groupRoll).child("members");
mGroupMembersDatabaseReference.addChildEventListener(new ChildEventListener() {
                        @Override
                        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                            UserDetails users = dataSnapshot.getValue(UserDetails.class);
                            Toast.makeText(GroupChatActivity.this, users.getRoll(), Toast.LENGTH_SHORT).show();
                            if (!users.getRoll().equals(senderRoll)) {
                                Toast.makeText(GroupChatActivity.this, users.getName(), Toast.LENGTH_SHORT).show();
                                mRecieverDatabaseReference = mRecieverDatabaseReference.child(users.getRoll()).child("groups").child(groupRoll);
                                Query query1 = mRecieverDatabaseReference.orderByChild("roll").equalTo(groupRoll);
                                query1.addListenerForSingleValueEvent(new ValueEventListener() {
                                    @Override
                                    public void onDataChange(DataSnapshot dataSnapshot) {
                                        for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
                                            dataSnapshot1.getRef().child("state").setValue("unread");
                                            dataSnapshot1.getRef().child("timeID").setValue(timeID);
                                        }
                                    }
                                    @Override
                                    public void onCancelled(DatabaseError databaseError) {
                                    }
                                });
                                Messages message = new Messages(messageText, time, senderRoll, name, chatName, groupRoll, mState, messageType, "");
                                mRecieverDatabaseReference.push().setValue(message);
                                mRecieverDatabaseReference = mDatabase.getReference().child("chats");
                            }
                        }
                        @Override
                        public void onChildChanged(DataSnapshot dataSnapshot, String s) {
                        }
                        @Override
                        public void onChildRemoved(DataSnapshot dataSnapshot) {
                        }
                        @Override
                        public void onChildMoved(DataSnapshot dataSnapshot, String s) {
                        }
                        @Override
                        public void onCancelled(DatabaseError databaseError) {
                        }
                    });

在此代码字符串中groupRoll = "1BME"

但是,当我运行此代码并进行调试时,我发现它从未进入onChildAdded()。所以请帮助我。该应用程序并没有因此而前进。

谢谢。

首先您的DatabaseReference是错误的。要从会员获取数据,请使用此代码:

mGroupMembersDatabaseReference = mDatabase.getReference().child("groups").child(groupRoll).child("members").child(memberId);

memberId是由push()方法生成的唯一ID。

第二,您无需使用addListenerForSingleValueEvent来更改值。在DataSnapshot上使用setValue()方法是错误的。您可以直接在reference上使用setValue()。希望它有帮助。

尝试此

for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
    UserDetails users = snapshot.getValue(UserDetails.class);
    //...
}

最新更新