无法将数据保存到Firebase数据库中(在Android中)。

huangapple go评论83阅读模式
英文:

Won't save data to Firebase Database in Android

问题

以下是您提供的代码部分的翻译:

public class SetupActivity extends AppCompatActivity {

    private EditText FullName, EmailAddress, Password, CountryName;
    private Button SaveInfoButton;
    private ProgressDialog LoadingBar;
    private CircleImageView ProfileImage;
    private FirebaseAuth register_auth;
    private DatabaseReference userreference;
    private String currentUserID;

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

        register_auth = FirebaseAuth.getInstance();
        currentUserID = register_auth.getCurrentUser().getUid();
        userreference = FirebaseDatabase.getInstance().getReference().child("Users").child(currentUserID);

        FullName = findViewById(R.id.name_setup);
        EmailAddress = findViewById(R.id.email_setup);
        Password = findViewById(R.id.password_setup);
        CountryName = findViewById(R.id.country_setup);
        SaveInfoButton = findViewById(R.id.save_button);
        ProfileImage = findViewById(R.id.profile_setup);
        LoadingBar = new ProgressDialog(this);

        SaveInfoButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CreateNewAccount();
            }
        });
    }

    private void CreateNewAccount() {
        // ... (省略部分代码)
    }

    private void SaveAccountInformation() {
        // ... (省略部分代码)
    }

    private void SendToLogin() {
        // ... (省略部分代码)
    }
}

如果有关代码的问题或需要进一步的帮助,请随时提问。

英文:

I'm not sure what the problem is. I'm a beginner developer and I coded a registration/login page for an Android app I'm working on. New users are saved in Firebase Authorization but not in Firebase Database. My current rules are set to false but when I try to set them to true, the app keeps returning to the SetupActivity rather than the MainActivity. The app works fine when the rules are set to false but as I said, nothing appears in the Database. Here is my code:

public class SetupActivity extends AppCompatActivity {

private EditText FullName, EmailAddress, Password, CountryName;
private Button SaveInfoButton;
private ProgressDialog LoadingBar;
private CircleImageView ProfileImage;
private FirebaseAuth register_auth;
private DatabaseReference userreference;
private String currentUserID;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setup);
register_auth = FirebaseAuth.getInstance();
currentUserID = register_auth.getCurrentUser().getUid();
userreference = FirebaseDatabase.getInstance().getReference().child("Users").child(currentUserID);
FullName = findViewById(R.id.name_setup);
EmailAddress = findViewById(R.id.email_setup);
Password = findViewById(R.id.password_setup);
CountryName = findViewById(R.id.country_setup);
SaveInfoButton = findViewById(R.id.save_button);
ProfileImage = findViewById(R.id.profile_setup);
LoadingBar = new ProgressDialog(this);
SaveInfoButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view)
{
CreateNewAccount();
}
});
}
private void CreateNewAccount() {
String full_name = FullName.getText().toString();
String email = EmailAddress.getText().toString();
String password = Password.getText().toString();
String country = CountryName.getText().toString();
if(TextUtils.isEmpty(email)) {
Toast.makeText(this, "Please enter email.", Toast.LENGTH_SHORT).show();
}
else if(TextUtils.isEmpty(full_name)) {
Toast.makeText(this, "Please enter your name.", Toast.LENGTH_SHORT).show();
}
else if(TextUtils.isEmpty(password)) {
Toast.makeText(this, "Please enter password.", Toast.LENGTH_SHORT).show();
}
else if(TextUtils.isEmpty(country)) {
Toast.makeText(this, "Please enter country.", Toast.LENGTH_SHORT).show();
}
else {
LoadingBar.setTitle("Creating new account!");
LoadingBar.setMessage("Please wait while your account is being created.");
LoadingBar.show();
LoadingBar.setCanceledOnTouchOutside(true);
register_auth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful()) {
LoadingBar.dismiss();
Toast.makeText(SetupActivity.this, "Registration was successful!", Toast.LENGTH_SHORT).show();
SaveAccountInformation();
}
else {
String message = task.getException().getMessage();
Toast.makeText(SetupActivity.this, "Registration unsuccessful." + message, Toast.LENGTH_SHORT).show();
LoadingBar.dismiss();
}
}
});
}
}
private void SaveAccountInformation() {
String full_name = FullName.getText().toString();
String country = CountryName.getText().toString();
Map<String, Object> childUpdates = new HashMap<>();
childUpdates.put("fullname", full_name);
childUpdates.put("country", country);
childUpdates.put("status", "Hey there, I am using Study Guide!");
childUpdates.put("birthday", "none");
userreference.updateChildren(childUpdates).addOnCompleteListener(new OnCompleteListener() {
@Override
public void onComplete(@NonNull Task task) {
if (task.isSuccessful()) {
SendToLogin();
}
else {
String message = task.getException().getMessage();
Toast.makeText(SetupActivity.this, "An error occurred. " + message, Toast.LENGTH_SHORT).show();
}
}
});
}
private void SendToLogin() {
Intent LoginIntent = new Intent(SetupActivity.this,LoginActivity.class);
LoginIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(LoginIntent);
finish();
}

}

If someone could point me in the right direction or let me know what I'm doing wrong, it will be very much appreciated!

答案1

得分: 1

Hazal,你不是在保存数据,而是在更新数据,所以要更改你的代码:

userreference.updateChildren(childUpdates)

userreference.setValue(childUpdates)
英文:

Hazal you are not saving the data , you are updating the data so change your code

from

userreference.updateChildren(childUpdates)

To

userreference.setValue(childUpdates)

答案2

得分: 0

你需要在新用户注册后,手动将用户保存到你的Firebase数据库中。

你可以查看文档以了解如何编写数据。

英文:

You need to manually save the users in your Firebase Database once a new user is registered.

You can look at the docs on how to write data.

huangapple
  • 本文由 发表于 2020年5月4日 16:21:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/61587924.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定