android - Adding a Custom ID to Firestore document - java

i"m working on a project that contains a code like this one: How to add Document with Custom ID to firestore (Angular)
When I'm about to try the app, it always crashes. But the code is all the same.
The error that I get in the LogCat is this:
Invalid document reference. Document references must have an even number of segments, but Users has 1
My full code is this:
public class RegisterActivity extends AppCompatActivity {
private EditText registerUsername;
private EditText registerEmail;
private EditText registerPassword;
private EditText registerConfirmPassword;
private Button registerRegisterButton;
private Button registerLoginButton;
private ProgressBar registerProgressBar;
private FirebaseFirestore firebaseFirestore;
private String user_id;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
registerUsername = findViewById(R.id.register_username);
registerEmail = findViewById(R.id.register_email);
registerPassword = findViewById(R.id.register_password);
registerConfirmPassword = findViewById(R.id.register_confirm_password);
registerRegisterButton = findViewById(R.id.register_register_button);
registerLoginButton = findViewById(R.id.register_login_button);
registerProgressBar = findViewById(R.id.register_progressBar);
firebaseFirestore = FirebaseFirestore.getInstance();
user_id = registerUsername.getText().toString();
registerLoginButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent loginIntent = new Intent(RegisterActivity.this, LoginActivity.class);
startActivity(loginIntent);
}
});
registerRegisterButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String username = registerUsername.getText().toString();
String email = registerEmail.getText().toString();
String password = registerPassword.getText().toString();
String confirmPassword = registerConfirmPassword.getText().toString();
if (!TextUtils.isEmpty(username) && !TextUtils.isEmpty(email) && !TextUtils.isEmpty(password) && !TextUtils.isEmpty(confirmPassword)) {
if (password.equals(confirmPassword)) {
registerProgressBar.setVisibility(View.VISIBLE);
Map<String, String> usersMap = new HashMap<>();
usersMap.put("username", username);
usersMap.put("email", email);
usersMap.put("password", password);
firebaseFirestore.collection("Users").document(user_id).set(usersMap).addOnSuccessListener(new OnSuccessListener<Void>() {
#Override
public void onSuccess(Void aVoid) {
Toasty.success(RegisterActivity.this, "Successfully Registered", Toast.LENGTH_SHORT).show();
Intent loginIntent = new Intent(RegisterActivity.this, LoginActivity.class);
startActivity(loginIntent);
registerProgressBar.setVisibility(View.INVISIBLE);
}
});
} else {
Toasty.error(RegisterActivity.this, "Passwords Don't Match", Toast.LENGTH_SHORT).show();
}
}
}
});
}}
I want the "user_id" to be the document id and NOT another id generated by Firestore.
Can someone please help? And Thanks in advance.

You're setting user_id at the beginning of your onCreate, probably when the EditText registerUsername doesn't contain any text. This means user_id will always be empty, no matter what happens in this activity.
You can't pass an empty string to document(). You need an actual string value there that's also a valid Firestore document ID. Try getting the string value in registerUsername after they've filled out the form, not before.

Related

Save current date and user Input to Firebase Firestore

I am making a note-saving app using Firebase Firestore and a RecyclerView.
So far users are able to enter the title and content of the note, this saves and works fine.
What I would like to add is the current date (in a "dd/MM/yy" format) when the user saves the note to be displayed in the recycler view the notes are displayed in.
My code for adding the note is as follows
public class AddWellbeingActivity extends AppCompatActivity {
private static final String TAG = "AddWellbeingActivity";
EditText createtitleofnote, createcontentofnote;
FloatingActionButton savenote;
FirebaseAuth firebaseAuth;
FirebaseUser firebaseUser;
FirebaseFirestore firebaseFirestore;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_wellbeing);
savenote = findViewById(R.id.saveNote);
createcontentofnote = findViewById(R.id.createContentNote);
createtitleofnote = findViewById(R.id.createTitleNote);
firebaseAuth = FirebaseAuth.getInstance();
firebaseFirestore = FirebaseFirestore.getInstance();
firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
savenote.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String title = createtitleofnote.getText().toString();
String content = createcontentofnote.getText().toString();
if(title.isEmpty() || content.isEmpty())
{
Toast.makeText(getApplicationContext(), "Both fields are Required", Toast.LENGTH_SHORT).show();
}
else
{
DocumentReference documentReference = firebaseFirestore.collection("notes").document(firebaseUser.getUid()).collection("myNotes").document();
Map<String, Object> note = new HashMap<>();
note.put("title",title);
note.put("content", content);
documentReference.set(note).addOnSuccessListener(new OnSuccessListener<Void>() {
#Override
public void onSuccess(Void aVoid)
{
Toast.makeText(getApplicationContext(), "Wellbeing entry saved", Toast.LENGTH_SHORT).show();
startActivity(new Intent(AddWellbeingActivity.this, WellbeingActivity.class));
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e)
{
Toast.makeText(getApplicationContext(), "Wellbeing entry failed to save", Toast.LENGTH_SHORT).show();
}
});
}
}
});
}
If anyone has a solution it would be greatly appreciated
To add a timestamp field when add a note into Firestore, then you should add an extra put() call:
note.put("title",title);
note.put("content", content);
note.put("date", FieldValue.serverTimestamp()); 👈

implementing clean architecture with realtime Firebase in AndroidStudio

Hi I want to implement clean architecture while using the realtime database but is seems that it is impossible to seperate the database from the my account screen class. Ideally I would want a DatabaseManager class that would handle all database operation (getting reading and posting to the db) but it seems that because the AuthStateChangeListener needs to be in the oncreate of my files it can't work. Does anyone know of any work-arounds? Here is the code I am using this on:
public class CreateAccountScreen extends AppCompatActivity {
private Button mRegister;
private EditText mEmail, mPassword, mCPassword, mName;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener firebaseAuthStateListener;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
setContentView(R.layout.create_account);
//------------------------------------------------------
//I would want to put this in a seperate class
//VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
//------------------------------------------------------
mAuth = FirebaseAuth.getInstance();
firebaseAuthStateListener = new FirebaseAuth.AuthStateListener() {
#Override
public void onAuthStateChanged(#NonNull FirebaseAuth firebaseAuth) {
final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user !=null){
Intent intent = new Intent(CreateAccountScreen.this, HomeScreen.class);
startActivity(intent);
finish();
return;
}
}
};
//-----------------------------------------------------
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//I would want this in a seperate class
//------------------------------------------------------
mRegister = (Button) findViewById(R.id.goSignIn2);
mEmail = (EditText) findViewById(R.id.username3);
mPassword = (EditText) findViewById(R.id.password2);
mCPassword = (EditText) findViewById(R.id.password3);
mName = (EditText) findViewById(R.id.username2);
mRegister.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
final String email = mEmail.getText().toString();
final String password = mPassword.getText().toString();
final String cPassword = mCPassword.getText().toString();
final String name = mName.getText().toString();
if (name.equals("") || password.equals("") || email.equals("") || !password.equals(cPassword)) {
Toast.makeText(CreateAccountScreen.this, "sign up error", Toast.LENGTH_SHORT).show();
return;
}
//------------------------------------------------------
//I would want to also put this in a seperate class
//VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
//------------------------------------------------------
//DatabaseManager.createUser(email, password, name, CreateAccountScreen.this);
mAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(CreateAccountScreen.this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if(!task.isSuccessful()) {
Toast.makeText(CreateAccountScreen.this, "sign up error", Toast.LENGTH_SHORT).show();
}else{
String userId = mAuth.getCurrentUser().getUid();
DatabaseReference currentUserDb = FirebaseDatabase.getInstance().getReference().child("Users").child(userId);
DatabaseReference currentUserDb2 = FirebaseDatabase.getInstance().getReference().child("Users").child(userId).child("connections").child("no").child(userId);
Map userInfo = new HashMap<>();
userInfo.put("name", name);
userInfo.put("profileImageUrl", "default");
currentUserDb2.setValue(true);
currentUserDb.updateChildren(userInfo);
}
}
});
//-----------------------------------------------------
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//I would want this in a seperate class
//------------------------------------------------------
}
});
}
}

Android application won't go the next activity from the log in or sign up activity

When i try to go to the next activity by pressing the sign_up in the sign up page or log in in the log page when the user already exists, the app just exits and brings up an error.
Sign up Activity
// variable
final String TAG ="signUp";
private MaterialEditText edit_name;
private MaterialEditText edit_password;
//DECLARING & INITIALISING BUTTON TO SIGN UP
FButton sign_Up;
final String KEY_NAME = "name";
final String KEY_PASSWORD ="password";
final String KEY_MARKS ="marks";
private FirebaseFirestore db = FirebaseFirestore.getInstance();
private DocumentReference user_reference = db.document("Users/Users details");
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.sign_up);
//INITIALISING THE EDITTEXT VIEWS
edit_name = findViewById(R.id.editName);
edit_password = findViewById(R.id.editPassword);
sign_Up = findViewById(R.id.btn_signUp);
sign_Up.setOnClickListener(view ->
{
user_reference.get()
.addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>()
{
Map<String, Object> users = new HashMap<>();
String name = edit_name.getText().toString();
String password = edit_password.getText().toString();
#Override
public void onSuccess(DocumentSnapshot documentSnapshot)
{
if(documentSnapshot.exists())
{ // GETTING INFORMATION FROM FIRESTORE DATABASE
name = documentSnapshot.getString(KEY_NAME);
password = documentSnapshot.getString(KEY_PASSWORD);
Toast.makeText(sign_up.this, "This user already exist, Try again", Toast.LENGTH_LONG).show();
}
else
{
users.put(KEY_NAME,name);
users.put(KEY_PASSWORD, password);
db.collection("Users").document(name).set(users);
Toast.makeText(sign_up.this,"Registered",Toast.LENGTH_LONG).show();
Intent i = new Intent(sign_up.this,sum_selection.class);
startActivity(i);
}
}// END OF ONSUCCESS
})
.addOnFailureListener(new OnFailureListener()
{
#Override
public void onFailure(#NonNull Exception e)
{
Toast.makeText(sign_up.this, "Error",Toast.LENGTH_LONG).show();
Log.d(TAG,e.toString());
}
});
Log in Activity
private MaterialEditText user_name;
private MaterialEditText user_password;
FButton sign_in;
private FirebaseFirestore database = FirebaseFirestore.getInstance();
private DocumentReference user_reference = database.document("Users/users details");
final String KEY_NAME = "name";
final String KEY_PASSWORD = "password";
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
sign_in = findViewById(R.id.btn_sign_in);
sign_in.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view) // log in button
{
user_reference.get()
.addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>()
{
#Override
public void onSuccess(DocumentSnapshot documentSnapshot)
{
if(documentSnapshot.exists())
{
Toast.makeText(log_in.this,"Welcome"+ user_name, Toast.LENGTH_LONG).show();
// ADD INTENT TO GO TO THE SUM_SELECTION PAGE
Intent in = new Intent(log_in.this, sum_selection.class);
startActivity(in);
}
else
{
// DISPLAY ERROR MESSAGE TO USER
Toast.makeText(log_in.this, "User does not exist", Toast.LENGTH_LONG).show();
}
}
})
.addOnFailureListener(new OnFailureListener()
{
#Override
public void onFailure(#NonNull Exception e)
{
Log.d(TAG, e.toString());
}
}); ```
[Error message][1]
[1]: https://i.stack.imgur.com/8gR62.png
Intent i = new Intent((this/activity),LoginActivity.class);
(this/activity).startActivity(i);
Check your intent properly, Intent take current activity as the first param and the activity you want to load up as the second param. Also, call startActivity(i) from the activity context becuase currently you are under the context of Firebase Listener.
Also, post the error log.

How to get data from text view in first activity and post it from another activity?

I want the data of textview in the second activity to pass in the third but i dont want to display it in the third activity. I want to directly send the data from the second activity to Pass in my sql db after clicking the button in the final activity. Is is achievable?
This is my first activity.
public class MainActivity extends AppCompatActivity {
private EditText email, password;
private Button btn_login;
private ProgressBar loading;
private static String URL_LOGIN ="http://192.168.1.83/Attendance/login.php";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
loading= findViewById(R.id.loading);
email = findViewById(R.id.editTextUserEmail);
password = findViewById(R.id.editTextPassword);
btn_login = findViewById(R.id.buttonRegister);
btn_login.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String mEmail = email.getText().toString().trim();
String mPassword = password.getText().toString().trim();
if (!mEmail.isEmpty() || !mPassword.isEmpty()) {
Login(mEmail, mPassword);
}else{
email.setError("Please Enter Email");
password.setError("Please Enter Password");
}
}
});
}
private void Login(final String email, final String password) {
loading.setVisibility(View.VISIBLE);
btn_login.setVisibility(View.GONE);
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL_LOGIN,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
Log.d("JSON", jsonObject.toString());
String success = jsonObject.getString("success");
JSONArray jsonArray =
jsonObject.getJSONArray("login");
if (success.equals("1"))
{
for (int i = 0; i<jsonArray.length(); i++){
JSONObject object =
jsonArray.getJSONObject(i);
String name =
object.getString("name").trim();
String email =
object.getString("email").trim();
// Toast.makeText(MainActivity.this,
"Success Login \nYour name: "+name+"\nYour Email: "+email,
// Toast.LENGTH_LONG).show();
Intent intent = new
Intent(MainActivity.this, HomeActivity.class);
intent.putExtra("name",name);
intent.putExtra("email", email);
startActivity(intent);
loading.setVisibility(View.GONE);
}
}
} catch (JSONException e) {
e.printStackTrace();
loading.setVisibility(View.GONE);
btn_login.setVisibility(View.VISIBLE);
Toast.makeText(MainActivity.this, "Error"
+e.toString(),
Toast.LENGTH_LONG).show();
}
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, "Error"
+error.toString(),
Toast.LENGTH_LONG).show();
}
})
{
#Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("email", email);
params.put("password", password);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
This is my second activity
public class HomeActivity extends AppCompatActivity {
private TextView name,email;
private TextView welcome;
private Button Send;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
name = findViewById(R.id.name);
email = findViewById(R.id.email);
Send = findViewById(R.id.btn_send);
welcome = findViewById(R.id.welcome);
final Intent intent = getIntent();
String extraName = intent.getStringExtra("name");
String extraEmail = intent.getStringExtra("email");
name.setText(extraName);
email.setText(extraEmail);
Send.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent1 = new Intent(HomeActivity.this,StatusActivity.class);
intent1.putExtra("wel", welcome.getText().toString());
intent1.putExtra("name1", name.getText().toString());
intent1.putExtra("email1", email.getText().toString());
startActivity(intent1);
}
});
}
And this is my final activity but Here i dont want to show the text view i just want to post it from here.
public class StatusActivity extends AppCompatActivity {
private TextView welcome, name, email;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_status);
welcome = findViewById(R.id.abcd);
name = findViewById(R.id.name1);
email = findViewById(R.id.email1);
final Intent intent = getIntent();
String extraName1 = intent.getStringExtra("name1");
String extraEmail1 = intent.getStringExtra("email1");
String extraWelcome = intent.getStringExtra("wel");
name.setText(extraName1);
email.setText(extraEmail1);
welcome.setText(extraWelcome);
}
Your question is pretty clear until you put MySql to the topic. Why you want to pass the data from one activity to another through the database. Why involve database operations which take time and resources.
You may pass data from one activity to another through Intents (primitive values, Strings and Parcelables)!
All you have to do is:
Get the data (int your case from TextView).
Create the Intent to navigate to the next Activity.
Put the data to the Intent.
Start the Activity.
Retrieve the data from the Intent.
For example if will navigate to the next Activity via button click then in that button's onClick():
// 1.
String data = myTextView.getText();
// 2.
Intent intent = new Intent(yourContext,YourNextActivity.class);
// 3.
intent.putStringExtra("MY DATA KEY",data);
// 4.
yourContext.startActivity(intent);
// 5. Now in YourNextActivity's onCreate();
String retrievedData = YourNextActivity.this.getIntent().getStringExtra("MY DATA KEY");
// Done!

Button works on Second Click in android

I am using cloud Firestore as database, when I am clicking on login button, it is not updating the value at first but on second click it updates.
When I click on Login, at first it takes empty values, but on second click it takes the value entered. Same happens when I change text and press login again, it takes previous values and on second click it takes updated values.
public class LoginActivity extends AppCompatActivity {
private TextView appName;
private TextInputEditText registrationNumber;
private TextInputEditText password;
private User userSent;
private FirebaseFirestore db;
private CollectionReference collectionReference;
String result="";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
db = FirebaseFirestore.getInstance();
db.enableNetwork();
collectionReference = db.collection("/user");
registrationNumber = findViewById(R.id.regno);
password = findViewById(R.id.password);
userSent = new User();
appName = findViewById(R.id.appname);
AssetManager assetManager = getApplicationContext().getAssets();
Typeface typeface = Typeface.createFromAsset(assetManager,"fonts/Ubuntu-Regular.ttf");
appName.setTypeface(typeface);
}
public void onLogin(View view){
userSent.setRegistrationNumber(registrationNumber.getText().toString());
userSent.setPassword(password.getText().toString());
if(!userSent.getRegistrationNumber().isEmpty() && !userSent.getPassword().isEmpty()) {
authenticateUser(userSent);
}
else{
Toast.makeText(this, "Registration Number or Password cannot be empty", Toast.LENGTH_LONG).show();
}
//Login Result
Log.d("Login Result: " , result);
if(result.contains("success")) {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
else if(result.length()>0){
Toast.makeText(this, result, Toast.LENGTH_LONG).show();
}
}
public void authenticateUser(final User sUser){
final String registration_number = sUser.getRegistrationNumber();
final String password = sUser.getPassword();
collectionReference.get()
.addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
#Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
for(QueryDocumentSnapshot queryDocumentSnapshot: queryDocumentSnapshots){
Map<String, Object> map;
map = queryDocumentSnapshot.getData();
String reg_no = (String) map.get("registration_number");
String pass = (String)map.get("password");
if(registration_number.equals(reg_no) && password.equals(pass)){
result = "success";
return;
}
else if(registration_number.equals(reg_no)){
result = "Incorrect Password";
return;
}
else{
result = "User does not exist";
}
}
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
result = "Error Connecting to Server";
}
});
}
}

Categories