So my problem is simple but Ihave not managed to find a solution. I have an ArrayList that stores the uid (user id) of users that have clicked a button. This data is then sent to firebase's Cloud Firestore. When user clicks the button, the users uid gets saved in the ArrayList. But when I sign in with other user and click the button, instead of adding the second user uid to the ArrayList, the second user's uid overwrites the whole ArrayList and only the second users uid gets saved on Cloud Firestore.
collectionReference = FirebaseFirestore.getInstance().collection("Fields");
collectionReference.document(venueID).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (!task.getResult().exists()){
//Here I declare the arrayList if the document doesn't exist
peopleHereThisUpdates = new ArrayList<>();
//The document that gets saved in Firestore,
// peopleHereThisUpdates is the main point in this question
Map<String, Object> field = new HashMap<>();
field.put(FIELD_NAME, venueName);
field.put(PEOPLE_HERE_KEY, peopleHere);
field.put(PEOPLE_HERE_BY_UID, peopleHereThisUpdates);
collectionReference.document(venueID).set(field);
The next code snippet is where I think the problem lies. As I declare the Arraylist as new Arraylist in the button press method, new arrayList is created every time the button is pressed. How can I prevent this from happening?
public void onButtonPressImHere() { //This gets called when button is pressed
peopleHereThisUpdates = new ArrayList<>(); //This is the possible problem
peopleHereThisUpdates.add(uid);
collectionReference.document(venueID).update(PEOPLE_HERE_BY_UID, peopleHereThisUpdates);
Whole Activity code,
public class DetailFieldActivity extends Activity{
public final String PEOPLE_HERE_KEY = "People Here";
public final String FIELD_NAME = "Field Name";
public final String CURRENT_FIELD = "Current Field";
public final String UID = "Uid";
public final String PEOPLE_HERE_BY_UID = "People here by uid";
ImageButton imTrainingHereButton;
int peopleHere;
//Get the size of this
FirebaseFirestore db = FirebaseFirestore.getInstance();
// The details of the venue that is being displayed.
String venueName;
private String venueID;
private CollectionReference collectionReference;
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
DocumentReference reference;
String uid = user.getUid();
ArrayList<String> peopleHereThisUpdates;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_field_detail);
getActionBar().setHomeButtonEnabled(true);
getActionBar().setDisplayHomeAsUpEnabled(true);
imTrainingHereButton = findViewById(R.id.imTrainingHereButton);
Bundle venue = getIntent().getExtras();
venueName = venue.getString("name");
venueID = venue.getString("ID");
setTitle(venueName);
TextView fieldName = findViewById(R.id.fieldName);
fieldName.setText(venueName);
collectionReference = FirebaseFirestore.getInstance().collection("Fields");
collectionReference.document(venueID).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (!task.getResult().exists()){
//Here I declare the arrayList if the document doesn't exist
peopleHereThisUpdates = new ArrayList<>();
//The document that gets saved in Firestore,
// peopleHereThisUpdates is the main point in this question
Map<String, Object> field = new HashMap<>();
field.put(FIELD_NAME, venueName);
field.put(PEOPLE_HERE_KEY, peopleHere);
field.put(PEOPLE_HERE_BY_UID, peopleHereThisUpdates);
collectionReference.document(venueID).set(field);
}
}
});
}
@Override
protected void onStart() {
super.onStart();
imTrainingHereButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onButtonPressImHere();
}
});
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
public void onButtonPressImHere() {
reference =
FirebaseFirestore.getInstance().collection("Users").document(uid);
Map<String, Object> users = new HashMap<>();
users.put(CURRENT_FIELD, venueName);
users.put(UID, uid);
reference.set(users);
peopleHereThisUpdates = new ArrayList<>();
peopleHereThisUpdates.add(uid);
collectionReference.document(venueID).update(PEOPLE_HERE_BY_UID,
peopleHereThisUpdates);
}
}