0

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);
    }
}
Juan Carlos Mendoza
  • 5,736
  • 7
  • 25
  • 50
Olli Toivanen
  • 103
  • 1
  • 8

2 Answers2

4

Yes you are right. Here on this line:

peopleHereThisUpdates = new ArrayList<>(); //This is the possible problem

a new ArrayList instance will be created each time the event is triggered. So remove this and initialize the peopleHereThisUpdates variable in the declaration: ArrayList<String> peopleHereThisUpdates = new ArrayList<>(); or move it out of the onButtonPressImHere method to some other place, like near the begining of the onCreate method:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    peopleHereThisUpdates = new ArrayList<>();
Juan Carlos Mendoza
  • 5,736
  • 7
  • 25
  • 50
  • Thanks for the answer. The problem just didn't fully go away. Every time I create a new user and try to press the button, the uid of the first user gets wiped away from the arrayList. @JuanCarlosMendoza – Olli Toivanen Mar 13 '18 at 04:45
  • Then I would recommend you to use a [debugger](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) to track what's going on with that list. – Juan Carlos Mendoza Mar 13 '18 at 13:11
  • Yeah, but I think the problem still lies in the ArrayList = new ArrayList declaration because every time the activity is created the ArrayList gets declared as new ArrayList onCreate. So when the button is pressed it adds the uid to a new, empty array. How can I initialize the ListArray in a way that it doesn't get created as a new ArrayList every time the Activity is launched? Thank you! – Olli Toivanen Mar 13 '18 at 15:52
0

just create view model for your activity and declare your arraylist there. when the activity is being destroyed, the value of arraylist in the view model will remain the same.