Reputation:
This is my Login Activity. Here the user is prompted to enter his details and the data is sent to a firebase database.
public class LoginActivity extends AppCompatActivity {
private EditText ET_fname;
private EditText ET_lname;
private EditText ET_NIC;
private EditText ET_email;
private EditText ET_mobile;
private Button signup_button;
private Firebase mRef;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Firebase.setAndroidContext(this);
signup_button = (Button) findViewById(R.id.signup_button);
ET_fname = (EditText) findViewById(R.id.ET_fname);
ET_lname = (EditText) findViewById(R.id.ET_lname);
ET_NIC = (EditText) findViewById(R.id.ET_NIC);
ET_email = (EditText) findViewById(R.id.ET_email);
ET_mobile = (EditText) findViewById(R.id.ET_mobile);
//Click Listener for button
signup_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Creating firebase object
Firebase ref = new Firebase("https://<database name>.firebaseio.com");
//Getting values to store
String firstname = ET_fname.getText().toString().trim();
String lastname = ET_lname.getText().toString().trim();
String NIC = ET_NIC.getText().toString().trim();
String email = ET_email.getText().toString().trim();
String mobile = ET_mobile.getText().toString().trim();
//Creating Person object
User user = new User(firstname, lastname, NIC, email, mobile);
//Adding values
user.setFirstname(firstname);
user.setLastname(lastname);
user.setNIC(NIC);
user.setEmail(email);
user.setMobile(mobile);
//Storing values to firebase
//ref.child("User").setValue(user);
ref.child("Users").child("User").setValue(user);
}
});private EditText ET_fname;
private EditText ET_lname;
private EditText ET_NIC;
private EditText ET_email;
private EditText ET_mobile;
private Button signup_button;
private Firebase mRef;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Firebase.setAndroidContext(this);
signup_button = (Button) findViewById(R.id.signup_button);
ET_fname = (EditText) findViewById(R.id.ET_fname);
ET_lname = (EditText) findViewById(R.id.ET_lname);
ET_NIC = (EditText) findViewById(R.id.ET_NIC);
ET_email = (EditText) findViewById(R.id.ET_email);
ET_mobile = (EditText) findViewById(R.id.ET_mobile);
//Click Listener for button
signup_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Creating firebase object
Firebase ref = new Firebase("https://<database name>.firebaseio.com");
//Getting values to store
String firstname = ET_fname.getText().toString().trim();
String lastname = ET_lname.getText().toString().trim();
String NIC = ET_NIC.getText().toString().trim();
String email = ET_email.getText().toString().trim();
String mobile = ET_mobile.getText().toString().trim();
//Creating Person object
User user = new User(firstname, lastname, NIC, email, mobile);
//Adding values
user.setFirstname(firstname);
user.setLastname(lastname);
user.setNIC(NIC);
user.setEmail(email);
user.setMobile(mobile);
//Storing values to firebase
//ref.child("User").setValue(user);
ref.child("Users").child("User").setValue(user);
}
});
This is my User Class
public class User {
private String firstname;
private String lastname;
private String NIC;
private String email;
private String mobile;
public User(String firstname, String lastname, String NIC, String email, String mobile) {
this.firstname = firstname;
this.lastname = lastname;
this.NIC = NIC;
this.email = email;
this.mobile = mobile;
}public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getNIC() {
return NIC;
}
public void setNIC(String nic) {
this.NIC = nic;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
}
The Login Activity is used to send the data in the text fields to a Firebase Database. And everytime I run the app, it replaces the user instance created.
I want to be able to create seperate instances for each user, by autogenerating a number or otherwise. Any idea on how to do this?
Upvotes: 0
Views: 733
Reputation: 599766
You're adding a user with this snippet:
//Creating Person object
User user = new User(firstname, lastname, NIC, email, mobile);
//Storing values to firebase
ref.child("Users").child("User").setValue(user);
With this you set the data at this location /Users/User
. If you want to add the data to a new unique child under that location, you use the Firebase push()
method:
//Storing values to firebase
ref.child("Users").child("User").push().setValue(user);
This will create a complex-looking key, called a push id. It has similar traits as the UUID that @NishantRoy uses in his answer, but has the additional benefit that they are chronologically ordered.
Note that this is covered in the Saving Lists Of Data section of the Firebase documentation. I highly recommend that you spend some time in that documentation before continuing as it provides answer to many common questions you may have.
Upvotes: 1
Reputation: 1141
There are 2 ways you could go about this:
1. FIREBASE AUTHENTICATION - My Recommendation
Firebase offers an authentication service that generates a uid
for every user created.
You can use this uid
as the key to store your user data in, and even add user authentication to your app.
2. Java's UUID method
If you DO want to create your own ID within the app, you can use the java.util.UUID.randomUUID()
method.
Example from TutorialsPoint:
import java.util.*;
public class UUIDDemo {
public static void main(String[] args) {
// creating UUID
UUID uid = UUID.fromString("38400000-8cf0-11bd-b23e-10b96e4ef00d");
// checking the value of random UUID
System.out.println("Random UUID value: "+uid.randomUUID());
}
}
prints out:
Random UUID value: 046b6c7f-0b8a-43b9-b35d-6489e6daee91
The consensus online seems to be that collisions are VERY low in this method, however, there is still a chance, so I'd recommend using Firebase's uid
because they guarantee it will be unique.
Upvotes: 0