I am new on Android Studio. I want to assign unique id (increment) from "1", but whenever I register account, I keep getting one only.
btn_register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String email = et_email.getText().toString().trim();
final String username = email.split("@")[0];
final String pwd = et_password.getText().toString().trim();
final long usr_id = 0; // not increment
if (email.isEmpty() || pwd.isEmpty()) {
et_email.setError("Entering Email Password Is required");
et_email.requestFocus();
return;
}
if (!Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
et_email.setError("Entering Valid Email is required");
}
if (pwd.length() < 3) {
et_password.setError("Password Length Should More Than 3");
et_password.requestFocus();
} else {
m_auth.createUserWithEmailAndPassword(email, pwd).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("users");
DatabaseReference usr_uid = ref.child(m_auth.getCurrentUser().getUid());
if (task.isSuccessful()) {
Toast.makeText(MainActivity.this, "Registration Successed", Toast.LENGTH_SHORT).show();
//User usr = new User(email,username,usr_id+1);
usr_uid.child("email").setValue(email);
usr_uid.child("username").setValue(username);
usr_uid.child("password").setValue(pwd);
usr_uid.child("icon").setValue("default_icon");
usr_uid.child("id").setValue(String.valueOf(usr_id+1));
Intent refer_to_home = new Intent(MainActivity.this, Home_Activity.class);
startActivity(refer_to_home);
finish();
}
else
Log.e("user_reg_fail", "Field Empty!");
}
});
}
}
});
And this is my model:
public class User {
private String email;
private String username;
private long usr_id=0;
private String icon;
public User(String email, String username, long id) {
this.email = username;
this.username = username;
this.usr_id = id;
}
public void setIcon(String icon) {
this.icon = icon;
}
public User() {
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public long getId() {
return usr_id;
}
public void setId(long id) {
this.usr_id = id;
}
}
The database I get
dldnPGvVsHeQmVqHkuJS4no4g1r1
|-email: "effeff@gmail.com"
|-icon: "default_icon"
|-id: "1" //from one
|-password: "xxx"
|-username: "effeff"
gWEvZMOgi7hmuU5xOXhlG3vibgj1
|-email: "feefee@gmail.com"
|-icon: "default_icon"
|-id: "1" // id is still one
|-password: "xxx"
|-username: "feefee"
I have no idea why it does not increment the ID. How can I able to get increment "2" "3"? I can able to register my account successfully, but it keeps only assign ID "1"
So inside your onClick
method you have those lines
final long usr_id = 0; // not increment
...
usr_uid.child("id").setValue(String.valueOf(usr_id+1));
every time that you click that button the usr_id
will set its values to 0.
With firebase you have a unique document id (example in your question is dldnPGvVsHeQmVqHkuJS4no4g1r1
, gWEvZMOgi7hmuU5xOXhlG3vibgj1
) you don't need any extra id
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.