Reputation: 54
Right now, I'm on an activity where a user has to input their first name, last name, and id into an EditText. The value they put in there is saved onto the Firestore Console. Here is my problem:
I open my app and sign in with google with my google account, let's call this account my primary account. I fill in the 3 EditTexts, and press on the save button that I named as "btnNextStep" (because it's meant to bring you to the next part of the form, but for now it is the "save" button). Once I press save, I can see the data I filled in, on Google Firestore. Now if I were to sign in to another Google account on the same device, call it my secondary account, and I fill in the EditTexts. Once I press save, instead of creating a new document in Firestore, it overwrites the primary account's data. The final product on Firestore is that it has the data from the secondary account saved, but the data from the primary account is now gone.
Here is the code to my .java file for this activity:
package com.example.attenda_attempt3;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.firestore.FirebaseFirestore;
import org.jetbrains.annotations.NotNull;
import java.util.HashMap;
import java.util.Map;
public class StudentInformationFormActivity extends AppCompatActivity {
Button btnNextStep;
EditText etFirstName;
EditText etLastName;
EditText etSchoolID;
FirebaseFirestore db = FirebaseFirestore.getInstance();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_student_information_form);
btnNextStep = findViewById(R.id.btnNextStep);
etFirstName = findViewById(R.id.etFirstName);
etLastName = findViewById(R.id.etLastName);
etSchoolID = findViewById(R.id.etSchoolID);
btnNextStep.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String firstName = etFirstName.getText().toString();
String lastName = etLastName.getText().toString();
String schoolID = etSchoolID.getText().toString();
Map<String, Object> data = new HashMap<>();
data.put("First Name", firstName);
data.put("Last Name", lastName);
data.put("School ID", schoolID);
db.collection("Users").document("User Information").set(data)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void unused) {
Toast.makeText(getApplicationContext(), "Saved", Toast.LENGTH_SHORT).show();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull @NotNull Exception e) {
Toast.makeText(getApplicationContext(), "Error Occurred, Data Not Saved", Toast.LENGTH_SHORT).show();
}
});
}
});
}
}
Can someone help me make it so everytime a user presses the "btnNextStep", it creates a new document and saves the data.
Upvotes: 0
Views: 130
Reputation: 598847
You'll need to generate a unique ID for each user and enter that into this line:
db.collection("Users").document("uniqueUserId").set(data)
If you'd be using Firebase Authentication, it'd automatically generate such a unique user ID for each user. But since you're not using any existing user management API (which is fine), you'll need to generate the ID yourself, associate it with the user in the database as shown above, and possibly also store it in local storage (like SharedPreferences
) to be able to restore it when the app restarts.
Upvotes: 1