首页 > 解决方案 > 如何获取要显示的数据?我的 Firebase 实时数据库仍然显示为空

问题描述

在此处输入图像描述 我正在尝试将我的 Realtime Firebase 数据库与我的代码同步。我目前正在使用 Firebase 身份验证,并且能够获取用户 ID,但我的实时数据库保持为空。我的 Firebase 实时数据库规则当前设置为 true。我的代码有问题吗?我正在尝试将用户 ID、名字和姓氏拉入 Firebase 数据库。

public class Add_Info_After_Registration extends AppCompatActivity {
    private EditText FirstName, LastName;
    private ImageButton RegisterInfoButton;
    private FirebaseAuth mAuth;
    private DatabaseReference UsersReference;

    //Firebase Things//
    String currentUserID;
    //Firebase Things//

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add__info__after__registration);

        //this is referring to storing username information in firebase//
        mAuth = FirebaseAuth.getInstance();
        currentUserID = mAuth.getCurrentUser().getUid();
        UsersReference = FirebaseDatabase.getInstance().getReference().child("Users").child(currentUserID);
        //this is referring to storing username information in firebase//

        FirstName = (EditText) findViewById(R.id.add_info_first_name);
        LastName = (EditText) findViewById(R.id.add_info_last_name);
        RegisterInfoButton = (ImageButton) findViewById(R.id.register_submit_button);
        mAuth = FirebaseAuth.getInstance();

        RegisterInfoButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                SaveAccountSetUpInformation();
                SendUserToMainActivity();
            }
        });
    }

    private void SaveAccountSetUpInformation()
    {
        String firstname = FirstName.getText().toString();
        String lastname = LastName.getText().toString();

        if(TextUtils.isEmpty(firstname))
        {
            Toast.makeText(this, "Please insert first name", Toast.LENGTH_SHORT).show();
        }

        if(TextUtils.isEmpty(lastname))
        {
            Toast.makeText(this, "Please insert last name", Toast.LENGTH_SHORT).show();
        }
        else
        {
            HashMap userMap = new HashMap();
            userMap.put("firstname", firstname);
            userMap.put("lastname", lastname);
            //this is referring to storing username information in firebase//    
            UsersReference.updateChildren(userMap).addOnCompleteListener(new OnCompleteListener() {
                @Override
                public void onComplete(@NonNull Task task)
                {
                    if(task.isSuccessful())
                    {
                        SendUserToMainActivity();
                        Toast.makeText(Add_Info_After_Registration.this, "Your Account is Created Sucessfully", Toast.LENGTH_LONG).show();
                    }
                    else
                    {
                        String message = task.getException().getMessage();
                        Toast.makeText(Add_Info_After_Registration.this, "Error Occured:"+ message, Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }
    }

    private void SendUserToMainActivity()
    {
        Intent setupIntent = new Intent(Add_Info_After_Registration.this, MainActivity.class);
        setupIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(setupIntent);
        finish();
    }
}

标签: javaandroidfirebasefirebase-realtime-database

解决方案


推荐阅读