Firebase数据库DataSnapshot无法正常运行

时间:2018-06-27 13:19:04

标签: java android firebase firebase-realtime-database

我正在尝试根据登录用户的性别向他们显示欢迎消息。如果为“男性”,则为setText("Hello Sir, your mail is: " + user.getEmail()),否则为setText("Hello Madam, your mail is: " + user.getEmail())。这是我的数据库结构: enter image description here

这是我正在处理的个人档案活动java:

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.support.v7.app.AppCompatActivity;
import com.firebase.client.DataSnapshot;
import com.firebase.client.Firebase;
import com.firebase.client.FirebaseError;
import com.firebase.client.ValueEventListener;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

public class ProfileActivity extends AppCompatActivity {
    TextView textViewUserEmail;

    private FirebaseDatabase mFirebaseDatabase;
    private FirebaseAuth mAuth;
    public DatabaseReference myRef;
    public String userID;

    @Override
    protected void onCreate(Bundle saveInstanceState) {
        super.onCreate(saveInstanceState);
        setContentView(R.layout.activity_profile);

        mAuth = FirebaseAuth.getInstance();
        Firebase.setAndroidContext(this);
        mFirebaseDatabase = FirebaseDatabase.getInstance();
        myRef = mFirebaseDatabase.getReference();
        final FirebaseUser user = mAuth.getCurrentUser();
        userID = user.getUid();
        DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("UsersMale");

        textViewUserEmail = (TextView) findViewById(R.id.textViewUserEmail);
        loadUserInformation();

        ref.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                String myStrign = dataSnapshot.getValue(String.class);
                if (user.isEmailVerified() && myStrign.equals("male")) {
                    textViewUserEmail.setText("Hello Sir, your mail is: " + user.getEmail());
                } else {
                    textViewUserEmail.setText("Hello Madam, your mail is: " + user.getEmail());
                }
            }

            @Override
            public void onCancelled(FirebaseError firebaseError) {}
        });
    }

}
}

代码返回错误。您能告诉我我在做什么错吗?预先感谢。

1 个答案:

答案 0 :(得分:4)

将数据库结构更改为此:

 users
   uniqueId
        name: superman
        gender: male
   uniqueId
        name: girl
        gender: female

不需要两个父节点UserMaleUserFemale,只需创建一个名为users的节点,每个用户将具有一个性别属性。

然后执行此操作:

FirebaseUser user = mAuth.getCurrentUser();
String userID = user.getUid();
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("users");

ref.child(userID).addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            String gender=dataSnapshot.child("gender").getValue().toString();
            if(gender.equals("male")) {
                textViewUserEmail.setText("Hello Sir, your mail is: " + user.getEmail());
            } else {
                textViewUserEmail.setText("Hello Madam, your mail is: " + user.getEmail());
            }
        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {}
    });
}

首先获取用户ID,然后将位置放置在users,然后附加一个侦听器并获取已登录用户的性别值。