我正在使用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()
。所以请帮助我。该应用程序因此没有向前发展。
谢谢。
答案 0 :(得分:0)
首先,您的DatabaseReference
是错误的。要从会员处获取数据,请使用以下代码:
mGroupMembersDatabaseReference = mDatabase.getReference().child("groups").child(groupRoll).child("members").child(memberId);
其中memberId
是push()
方法生成的唯一ID。
其次,您不需要使用addListenerForSingleValueEvent
来更改值。在setValue()
上使用DataSnapshot
方法是错误的。您可以直接在setValue()
上使用reference
。希望它有所帮助。
答案 1 :(得分:0)
试试这个
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
UserDetails users = snapshot.getValue(UserDetails.class);
//...
}