我的Firebase数据库为每个成员属性返回null

时间:2018-08-23 07:23:25

标签: java android firebase firebase-realtime-database

我创建了一个Firebase数据库来像存储联系人一样存储成员,我试图获取存储在数据库中的所有成员属性。现在,成员列表可以正确提取,但是成员详细信息,例如phoneemail等返回null。请帮帮我。

这是Firebase数据库的结构:

Firebase database Structure

这是我获取成员列表的代码:

members = new ArrayList<>();
    member = new Member();
    adapter = new AllMembersAdapter(members, this);

    database = FirebaseDatabase.getInstance();
    databaseReference = database.getReference().child("Rad5 Tech Hub");

    databaseReference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            members.clear();

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

                member = ds.getValue(Member.class);
                Log.d("debugger", "member email: " + member.getEmail()); //returns null
                members.add(member);

            }

            recyclerViewList.setAdapter(adapter);
        }

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

        }
    });

这是我的会员舱

public class Member {

private String mMonthName;
private String mLastName;
private String mGender;
private String mEmail;
private String mPhone;
private String mAddress;
private String mDate;
private String mOccupation;

public Member(){}

public Member(String mAddress, String mDate, String mEmail, String mGender,
              String mLastName, String mMonthName, String mOccupation, String mPhone) {
    this.mLastName = mLastName;
    this.mGender = mGender;
    this.mEmail = mEmail;
    this.mPhone = mPhone;
    this.mAddress = mAddress;
    this.mDate = mDate;
    this.mOccupation = mOccupation;
    this.mMonthName = mMonthName;
}

public String getLastName() {
    return mLastName;
}

public String getGender() {
    return mGender;
}

public String getEmail() {
    return mEmail;
}

public String getPhone() {
    return mPhone;
}

public String getAddress() {
    return mAddress;
}

public String getDate() {
    return mDate;
}

public String getMonthName() {
    return mMonthName;
}

public String getOccupation() {
    return mOccupation;
}

public Map<String, Object> toMap(){
    HashMap<String, Object> result = new HashMap<>();
    result.put("address", mAddress);
    result.put("birthday", mDate);
    result.put("email", mEmail);
    result.put("gender", mGender);
    result.put("last name", mLastName);
    result.put("month name", mMonthName);
    result.put("occupation", mOccupation);
    result.put("phone number", mPhone);

    return result;
}

}

这是我的适配器类:

public class AllMembersAdapter extends RecyclerView.Adapter<AllMembersAdapter.MembersViewHolder> {

ArrayList<Member> members;
ListItemClickListener listItemClickListener;

public AllMembersAdapter(ArrayList<Member> members, ListItemClickListener listItemClickListener) {
    this.members = members;
    this.listItemClickListener = listItemClickListener;
}

@NonNull
@Override
public MembersViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    Context context = parent.getContext();
    LayoutInflater inflater = LayoutInflater.from(context);

    View view = inflater.inflate(R.layout.member_item, parent, false);
    return new MembersViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull MembersViewHolder holder, int position) {
    Member member = members.get(position);
    String _monthName = member.getMonthName();

    holder.mMonth.setText(_monthName);
    holder.mName.setText(member.getLastName());
    holder.mDate.setText(member.getDate());
}

@Override
public int getItemCount() {
    return members.size();
}

public interface ListItemClickListener{
    void onListItemClick(int position);
}

public class MembersViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

    TextView mMonth;
    TextView mName;
    TextView mDate;

    public MembersViewHolder(View itemView) {
        super(itemView);

        mMonth = (TextView) itemView.findViewById(R.id.txt_month);
        mName = (TextView) itemView.findViewById(R.id.txt_name);
        mDate = (TextView) itemView.findViewById(R.id.txt_date);
        itemView.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        listItemClickListener.onListItemClick(getAdapterPosition());
    }
}

}

1 个答案:

答案 0 :(得分:2)

正如@TheTechWolf在他的评论中提到的那样,问题在于Member类字段与Firebase数据库中具有的字段名称不同。让我们举个例子,在您的模型类中,有一个名为mMonthName的字段,但在数据库中,相应的字段名为month name。当您使用名为getMonthName()的吸气剂时,Firebase会照顾一个名为monthName而不是像现在这样的mMonthName的字段。因此,为解决此问题,请使用以下模型类,其中包含您需要的所有成员字段:

public class Member {
    private String monthName, lastName, gender, email, phone, address, date, occupation;

    public Member() {}

    public Member(String monthName, String lastName, String gender, String email, String phone, String address, String date, String occupation) {
        this.monthName = monthName;
        this.lastName = lastName;
        this.gender = gender;
        this.email = email;
        this.phone = phone;
        this.address = address;
        this.date = date;
        this.occupation = occupation;
    }

    public String getMonthName() { return monthName; }
    public String getLastName() { return lastName; }
    public String getGender() { return gender; }
    public String getEmail() { return email; }
    public String getPhone() { return phone; }
    public String getAddress() { return address; }
    public String getDate() { return date; }
    public String getOccupation() { return occupation; }
}