从firebase检索用户数据时出现问题

时间:2017-12-12 09:21:40

标签: android firebase firebase-realtime-database

我正在尝试从firebase数据库获取数据,其中数据存在于用户内部。我想检索数据并将其放在模型类中。我希望输出作为来自firebase的数据文本并通过电子邮件发送给用户,但是在电子邮件中传递的值为null。数据检索存在问题。

mAuth = FirebaseAuth.getInstance();
    mAuthListener = new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull final FirebaseAuth firebaseAuth) {
            user = firebaseAuth.getCurrentUser();

                // User is signed in
                Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());

                database = FirebaseDatabase.getInstance().getReference().child("users/"+user.getUid()+"/delivery");
                ValueEventListener postListener = new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        post= dataSnapshot.getValue(RetrieveData.class);
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                };
                database.addValueEventListener(postListener);
        }
    };

RetrieveData.java

public class RetrieveData {
    private String city, country, email, postal, state, street, username;

    public RetrieveData(String city, String country, String email, String postal, String state, String street, String username) {
        this.city = city;
        this.country = country;
        this.email = email;
        this.postal = postal;
        this.state = state;
        this.street = street;
        this.username = username;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPostal() {
        return postal;
    }

    public void setPostal(String postal) {
        this.postal = postal;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getStreet() {
        return street;
    }

    public void setStreet(String street) {
        this.street = street;
    }

    @Override
    public String toString() {
        return "RetrieveData{" +
                "city='" + city + '\'' +
                ", country='" + country + '\'' +
                ", email='" + email + '\'' +
                ", postal='" + postal + '\'' +
                ", state='" + state + '\'' +
                ", street='" + street + '\'' +
                ", username='" + username + '\'' +
                '}';
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }
}

Here is firebase database image.

2 个答案:

答案 0 :(得分:1)

要获取该数据,请使用以下代码:

String uid = firebaseAuth.getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference deliveryRef = rootRef.child("users").child(uid).child("delivery");
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        String city = dataSnapshot.child("city").getValue(String.class);
        String country = dataSnapshot.child("country").getValue(String.class);
        String email = dataSnapshot.child("email").getValue(String.class);
        String postal = dataSnapshot.child("postal").getValue(String.class);
        String state = dataSnapshot.child("state").getValue(String.class);
        String street = dataSnapshot.child("street").getValue(String.class);
        String username = dataSnapshot.child("username").getValue(String.class);
        Log.d("TAG", city + " / " + 
                 country + " / " + 
                 email + " / " + 
                 postal + " / " + 
                 state + " / " + 
                 street + " / " + 
                 username);
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
deliveryRef.addListenerForSingleValueEvent(eventListener);

拥有这些价值观,你可以用它做任何你想做的事。

这是使用RetrieveData class:

的对象的另一种方法
String uid = firebaseAuth.getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference deliveryRef = rootRef.child("users").child(uid).child("delivery");
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        RetrieveData retrieveData = dataSnapshot.getValue(RetrieveData.class);
        Log.d("TAG", retrieveData);
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
deliveryRef.addListenerForSingleValueEvent(eventListener);

答案 1 :(得分:0)

 DatabaseReference databaseReference = 
FirebaseDatabase.getInstance().getReference("Users");

     databaseReference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot snapshot) {

            for (DataSnapshot postSnapshot : snapshot.getChildren()) {
                //Getting the data from snapshot
                // user is a model class
                User person = postSnapshot.getValue(User.class);

                Log.e("Üser Deatils", String.valueOf(postSnapshot.getValue(User.class)));
              //  https://console.firebase.google.com/project/samplechat-611e3/database/samplechat-611e3/data
                //add person to your list
                registerUserArrayList.add(person);


            }
            linearLayoutManager = new LinearLayoutManager(UserActivity.this);
            recyclerView.setLayoutManager(linearLayoutManager);
            recyclerView.setHasFixedSize(true);
            registerUserAdpater = new RegisterUserAdpater(UserActivity.this,registerUserArrayList);
            recyclerView.setAdapter(registerUserAdpater);

        }
        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
 });

 Hope its help you to retrieve your all user details.