0

I want to get the idNumber from the database and get the max value, then add it by 1 to increment it to the new id number base on year of birth. Is there a way for me to get the variable inside addValueEventListener to pass it to the constructor? If not, are there any other ways?

  int newIdNumberInt = 0;
  int idList = 0;
  int max = 0;
            
            setNewIdYear = newBirthday.substring(newBirthday.length() - 4);
            setNewReg = "000";
            setNewIdNumber = setNewIdYear + setNewReg;


            newIdNumberInt = Integer.parseInt(setNewIdNumber);

            mDatabase.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot snapshot) {
                    if (snapshot.exists()) {
                        for (DataSnapshot dataSnapshot : snapshot.getChildren()) {
                            Map<String, Object> map = (Map<String, Object>) dataSnapshot.getValue();
                            Object id = map.get("idNumber");
                            String newOb = String.valueOf(id);

                            try {

                                List<Integer> listObject = new ArrayList<>();
                                if (newOb.startsWith(setNewIdYear))

                                    idList = Integer.valueOf(newOb);

                                listObject.add(idList);
                                max = Collections.max(listObject);

                                newIdNumberInt = max + 1;

                            } catch (ClassCastException e) {
                            }
                        }
                    }
                }

                @Override
                public void onCancelled(@NonNull DatabaseError error) {

                }
            });


            mCustom = new Custom(newIdNumberInt, newFirstName, newMiddleName, newLastName, newAddress, newBirthday);
            key = mDatabase.child("Custom").push().getKey();
            mDatabase.child(newFirstName + "-" + newLastName + "-" + randomKey).setValue(mCustom);
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Rho
  • 29
  • 5
  • checkout : https://stackoverflow.com/questions/55222414/increment-existing-value-in-firebase-firestore – Ashvin solanki Jun 06 '22 at 07:48
  • Since data is loaded from Firebase asynchronously, all code that needs that data has to be *inside* `onDataChange`, be called from there, or otherwise synchronized. So move the code that creates the new `Custom` object (and writes it to the database) to the end of `onDataChange`. Also see https://stackoverflow.com/questions/50434836/getcontactsfromfirebase-method-return-an-empty-list/50435519#50435519 and https://stackoverflow.com/questions/33203379/setting-singleton-property-value-in-firebase-listener – Frank van Puffelen Jun 06 '22 at 12:16
  • [This question](https://stackoverflow.com/a/70178210/9473786) may also be useful – Tyler V Jun 06 '22 at 12:23
  • You might also be interested in reading this [resource](https://medium.com/firebase-tips-tricks/how-to-read-data-from-firebase-realtime-database-using-get-269ef3e179c5). – Alex Mamo Jun 06 '22 at 12:54
  • I really did moved the Custom object at the end of onDataChange but it crashes. There was no error when I checked run. – Rho Jun 06 '22 at 13:35

0 Answers0