gpt4 book ai didi

java - 使用 FirebaseDatabase 检索数据

转载 作者:太空宇宙 更新时间:2023-11-04 12:26:16 25 4
gpt4 key购买 nike

我使用 firebase 数据库,在我的应用程序中我将一些数据放入数据库中,但问题是我没有检索这些数据。使用此代码,我希望使用唯一的用户名进行注册,但是当我将数据放入注册 Activity 时,具有相同用户名的信息将被更新。在代码开头,FirebaseDatabase 数据库等于“null”,但我不明白为什么。

我的 firebase 数据结构:

enter image description here

注册.java:

    public class Registration extends AppCompatActivity {
EditText ET_USER_LOCATION, ET_USER_MAIL, ET_USER_NAME, ET_USER_PASS;
String method, user_location, user_mail, user_name, user_pass;
Button registerButton;
Context mContext;
String token = FirebaseInstanceId.getInstance().getToken();

FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference users = database.getReference("UserApp");

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_registration);
mContext = this;

ET_USER_LOCATION = (EditText) findViewById(R.id.new_user_location);
ET_USER_MAIL = (EditText) findViewById(R.id.new_user_mail);
ET_USER_NAME = (EditText) findViewById(R.id.new_user_name);
ET_USER_PASS = (EditText) findViewById(R.id.new_user_pass);

registerButton = (Button) findViewById(R.id.registerButton);

registerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
user_location = ET_USER_LOCATION.getText().toString();
user_mail = ET_USER_MAIL.getText().toString();
user_name = ET_USER_NAME.getText().toString();
user_pass = ET_USER_PASS.getText().toString();


DatabaseReference name = users.child("user_name");
name.addValueEventListener(new ValueEventListener() {
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String user = dataSnapshot.getValue(String.class);
if(!Objects.equals(user, user_name)){
writeNewUser(user_name, user_mail, user_pass, user_location, token);
} else {
Toast.makeText(mContext, "This username is already registered. Try again!", Toast.LENGTH_SHORT).show();
}
}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});

finish();
}
});
}


public void userReg(){




}


private void writeNewUser(String user_name, String user_mail, String user_pass, String user_location, String user_token) {
UserApp user = new UserApp(user_name, user_mail, user_pass, user_location, user_token);

users.child(user_name).setValue(user);
}





}

UserApp.java:

   @IgnoreExtraProperties
public class UserApp {

public String user_name;
public String user_mail;
public String user_pass;
public String user_location;
public String user_token;

public UserApp() {
// Default constructor required for calls to DataSnapshot.getValue(User.class)
}

public UserApp(String user_name, String user_mail, String user_pass, String user_location, String user_token) {

this.user_name = user_name;
this.user_mail = user_mail;
this.user_pass = user_pass;
this.user_location = user_location;
this.user_token = user_token;
}


}

提前感谢您的帮助。

最佳答案

我找到了解决方案:

注册.java:

public class Registration extends AppCompatActivity {
EditText ET_USER_LOCATION, ET_USER_MAIL, ET_USER_NAME, ET_USER_PASS;
String method, user_location, user_mail, user_name, user_pass;
Button registerButton;
Context mContext;

FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference users = database.getReference("UserApp");
String key = users.child("UserApp").push().getKey();
String token = FirebaseInstanceId.getInstance().getToken();


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_registration);
mContext = this;

ET_USER_LOCATION = (EditText) findViewById(R.id.new_user_location);
ET_USER_MAIL = (EditText) findViewById(R.id.new_user_mail);
ET_USER_NAME = (EditText) findViewById(R.id.new_user_name);
ET_USER_PASS = (EditText) findViewById(R.id.new_user_pass);

registerButton = (Button) findViewById(R.id.registerButton);

registerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
user_location = ET_USER_LOCATION.getText().toString();
user_mail = ET_USER_MAIL.getText().toString();
user_name = ET_USER_NAME.getText().toString();
user_pass = ET_USER_PASS.getText().toString();

users.addValueEventListener(new ValueEventListener() {
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String result = registerWithoutRepetition(dataSnapshot);
Toast.makeText(mContext, result, Toast.LENGTH_SHORT).show();
if(Objects.equals(result, "Login Success")){
finish();
}
}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});

}
});
}





private void writeNewUser(String user_id, String user_name, String user_mail, String user_pass, String user_location, String user_token) {
UserApp user = new UserApp(user_name, user_mail, user_pass, user_location, user_token);

users.child(user_id).setValue(user);
}

@RequiresApi(api = Build.VERSION_CODES.KITKAT)
private String registerWithoutRepetition(DataSnapshot dataSnapshot){
for(DataSnapshot snapshot : dataSnapshot.getChildren()) {
UserApp userApp = snapshot.getValue(UserApp.class);
if (Objects.equals(userApp.user_name, user_name)) {
return "Username is already registered, try again";
}
}
writeNewUser(key, user_name, user_mail, user_pass, user_location, token);
return "Login Success";
}

关于java - 使用 FirebaseDatabase 检索数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38363534/

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