gpt4 book ai didi

java - Android firebase 实时写入新用户

转载 作者:行者123 更新时间:2023-11-29 19:01:56 25 4
gpt4 key购买 nike

编辑:你们能教我如何在包中添加用户类吗?我不知道怎么办

你好,我是这方面的初学者,但我能问一下为什么 writenewuser User user = new user 说无法解析符号用户吗?我打算在数据库上添加登录详细信息,然后在它们上设置一个值 1 或 2,这样我就可以选择注册用户想要去的仪表板。但是我很困惑,因为每次我使用 public void writenewuser 搜索指南或示例时。 “User user = new user”代码总是报错“Cannot resolve symbol User”

public class signup extends AppCompatActivity {
private EditText inputEmail, inputPassword, inputName;
private Button btnSignIn, btnSignUp, btnResetPassword;
private ProgressBar progressBar;
private FirebaseAuth auth;
private DatabaseReference mFirebaseDatabase;
private FirebaseDatabase mFirebaseInstance;
private String userId;
private int type =1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signup);
// Displaying toolbar icon
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setIcon(R.mipmap.ic_launcher);
//Get Firebase auth instance
auth = FirebaseAuth.getInstance();
btnSignIn = (Button) findViewById(R.id.sign_in_button);
btnSignUp = (Button) findViewById(R.id.sign_up_button);
inputEmail = (EditText) findViewById(R.id.email);
inputPassword = (EditText) findViewById(R.id.password);
inputName = (EditText) findViewById(R.id.name);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
btnResetPassword = (Button) findViewById(R.id.btn_reset_password);

mFirebaseInstance = FirebaseDatabase.getInstance();
// get reference to 'users' node
mFirebaseDatabase = mFirebaseInstance.getReference("Users");

btnResetPassword.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(signup.this, ResetPasswordActivity.class));
}
});
btnSignIn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
btnSignUp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

String email = inputEmail.getText().toString().trim();
String password = inputPassword.getText().toString().trim();
final String name = inputName.getText().toString().trim();
if (TextUtils.isEmpty(email)) {
Toast.makeText(getApplicationContext(), "Enter email address!", Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(password)) {
Toast.makeText(getApplicationContext(), "Enter password!", Toast.LENGTH_SHORT).show();
return;
}
if (password.length() < 6) {
Toast.makeText(getApplicationContext(), "Password too short, enter minimum 6 characters!", Toast.LENGTH_SHORT).show();
return;
}
progressBar.setVisibility(View.VISIBLE);
//create user
auth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(signup.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Toast.makeText(signup.this, "createUserWithEmail:onComplete:" + task.isSuccessful(), Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.GONE);
// If sign in fails, display a message to the user. If sign in succeeds
// the auth state listener will be notified and logic to handle the
// signed in user can be handled in the listener.
if (!task.isSuccessful()) {
Toast.makeText(signup.this, "Authentication failed." + task.getException(),
Toast.LENGTH_SHORT).show();
} else {
finish();
onAuthSuccess(task.getResult().getUser());
String user_id = auth.getCurrentUser().getUid();
DatabaseReference user_details = mFirebaseDatabase.child(user_id);
user_details.child("name").setValue(name);
user_details.child("type").setValue("1");
Intent mainIntent = new Intent(signup.this, LoginActivity.class);
mainIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(mainIntent);

}
}

});


}
private void onAuthSuccess(FirebaseUser user) {
String username = usernameFromEmail(user.getEmail());

// Write new user
writeNewUser(user.getUid(), username, user.getEmail());

// Go to MainActivity
startActivity(new Intent(signup.this, LoginActivity.class));
finish();
}
private String usernameFromEmail(String email) {
if (email.contains("@")) {
return email.split("@")[0];
} else {
return email;
}
}
private void writeNewUser(String userId, String name, String email) {
User user = new User(name, email);

mFirebaseDatabase.child("Users").child(userId).setValue(user);
}

});



}

@Override
protected void onResume() {
super.onResume();
progressBar.setVisibility(View.GONE);
}

最佳答案

你需要像这个例子一样创建类 User:

public class User{
private String name,email;

public User(){

}

public User(String name, String email){
this.name=name;
this.email=email;
}
public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getEmail() {
return email;
}

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

}

关于java - Android firebase 实时写入新用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48718269/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com