我使用firebase数据库,我的应用程序我在数据库中放了一些数据,但问题是我没有检索这些。
使用此代码我希望使用唯一的用户名进行注册,但是当我使用我的注册活动放置数据时,将更新具有相同用户名的信息。
在代码的开头,FirebaseDatabase database is equal to "null"
但我不明白为什么。
我的firebase数据结构:
Registration.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;
}
}
提前感谢您的帮助。
答案 0 :(得分:0)
我找到了解决方案:
<强> Registration.java:强>
guard let indexPath = indexPath, newIndexPath = newIndexPath else { return }
_tableView?.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Fade)
_tableView?.insertRowsAtIndexPaths([newIndexPath], withRowAnimation: UITableViewRowAnimation.Fade)