每当我尝试从Firebase获取用户名时,它都会返回值,但不会在String中更新

时间:2018-03-07 07:33:26

标签: java android firebase firebase-realtime-database

这是我的登录活动。每当我点击登录按钮它成功登录我另一方面我也试图从数据库中获取用户名和配置文件名称,它成功完全返回值。主要问题是,当Firebase返回值时,它不会更新到我的字符串中,并在我尝试检索它时显示为null。

public class LoginActivity extends AppCompatActivity {

    FirebaseAuth firebaseAuth = FirebaseAuth.getInstance ();
    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference myRef = database.getReference ();
    String[] z= {""};
    EditText email,password;
    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView (R.layout.activity_login);
        email = findViewById (R.id.editUser);
        password = findViewById (R.id.editPass);
        final SharedPreferences sharedPreferences = getSharedPreferences ("Login", MODE_PRIVATE);
        final SharedPreferences sharedPreferences2 = getSharedPreferences ("isLoginned", MODE_PRIVATE);
        final SharedPreferences.Editor editor = sharedPreferences.edit ();
        final SharedPreferences.Editor editor2 = sharedPreferences2.edit ();
        Button signin = findViewById (R.id.signin);
        final ProgressDialog dialog = new ProgressDialog (LoginActivity.this);

        dialog.setMessage ("Loging in.  Please wait");
        dialog.setTitle ("Login User");
        dialog.setCancelable (false);
        signin.setOnClickListener (new View.OnClickListener () {
            @Override
            public void onClick(View view) {
                dialog.show ();
                if(!email.getText ().toString ().equals (null) && !password.getText ().toString ().equals (null)){
                    firebaseAuth.signInWithEmailAndPassword (email.getText ().toString (),password.getText ().toString ()).addOnCompleteListener (new OnCompleteListener<AuthResult> () {
                        @Override
                        public void onComplete(@NonNull Task<AuthResult> task) {
                            if(task.isSuccessful ()){
                                int a = email.getText ().toString ().indexOf ("@");
                                SplashScreen.USERNAME = email.getText ().toString ().substring (0,a);
                                myRef.addValueEventListener(new ValueEventListener() {
                                    @Override
                                    public void onDataChange(DataSnapshot dataSnapshot) {
                                        DataSnapshot snapshot = dataSnapshot.child ("User").child (SplashScreen.USERNAME);
                                        DataSnapshot usernamee = snapshot.child ("Username");
                                        DataSnapshot name = snapshot.child ("Name");
                                        SplashScreen.IDNAME = String.valueOf (usernamee.getValue ());
                                        SplashScreen.NAME = String.valueOf (name.getValue ());
                                        editor.putString ("ussername", SplashScreen.IDNAME);
                                        editor.putString ("name", SplashScreen.NAME);
                                        editor.apply ();
                                    }

                                    @Override
                                    public void onCancelled(DatabaseError error) {
                                        Toast.makeText (LoginActivity.this, error.getMessage (), Toast.LENGTH_SHORT).show ();
                                    }
                                });
                                dialog.dismiss ();
                                editor.putString ("Email",email.getText ().toString ());
                                editor.putString ("pass",password.getText ().toString ());
                                editor.putString ("Username",SplashScreen.USERNAME);
                                editor2.putString ("login","Yes");
                                editor.apply ();
                                editor2.apply ();
                                editor.commit ();
                                editor2.commit ();
                                Intent intent = new Intent (LoginActivity.this, WallOfTheApp.class);
                                startActivity (intent);
                                finish ();
                            }
                        }
                    }).addOnFailureListener (new OnFailureListener () {
                        @Override
                        public void onFailure(@NonNull Exception e) {
                            dialog.dismiss ();
                            Toast.makeText (LoginActivity.this, e.getMessage (), Toast.LENGTH_SHORT).show ();
                        }
                    });
                }
            }
        });
    }
}

问题出在这样的背景下:

DataSnapshot snapshot = dataSnapshot.child ("User").child (SplashScreen.USERNAME);
                                        DataSnapshot usernamee = snapshot.child ("Username");
                                        DataSnapshot name = snapshot.child ("Name");
                                        SplashScreen.IDNAME = String.valueOf (usernamee.getValue ());
                                        SplashScreen.NAME = String.valueOf (name.getValue ());
                                        editor.putString ("ussername", SplashScreen.IDNAME);
                                        editor.putString ("name", SplashScreen.NAME);
                                        editor.apply ();

这是我的Firebase数据库:

{
  "User" : {
    "prathamkhurana43" : {
      "Mobile Number" : "1234567890",
      "Name" : "Pratham",
      "Username" : "prathamk22"
    }
  }
}
  

每当我第一次点击按钮时,它都不会获得值但是会打开   第二次点击登录按钮获取我的值   用户名和个人资料名称

1 个答案:

答案 0 :(得分:1)

试试这个:

DatabaseReference myRef = database.getReference();
myRef.child("User").addValueEventListener(new ValueEventListener() {
     @Override
   public void onDataChange(DataSnapshot dataSnapshot) {
    for(DataSnasphot datas: dataSnapshot.getChildren()){
    String names=datas.child("Name").getValue().toString();
    String usernames=datas.child("Username").getValue().toString();
    editor.putString ("ussername",names);
    editor.putString ("name", usernames);
    editor.apply();
   }

这会为您提供可在共享偏好设置中使用的值nameusername

如果你有这个值"prathamkhurana43",你也可以这样做:

myRef.child("User").child("prathamkhurana43").addValueEventListener(new ValueEventListener() {
  //retrieve data without using the for loop