How to make Firebase report changes to me ONLY when I click on a button? Android Studio

-1

I have a problem with the Firebase in Android Studio.

I have 2 buttons on my interface, one that is called "Upload Data" and another one called "Get Data". When I click on the "Upload Data" button, it uploads the data perfectly to the Firebase, however, when I click on the "Get Data" button, I program it to generate a PDF report of all the Firebase information, and the first time I hit the "Get Data" button if it gives me what I want, but when I hit the "Upload Data" button again it uploads the information to the Firebase, then updates the Firebase and inadvertently shows me the previously created PDF (THIS IS MY PROBLEM), it makes sense, since the Firebase was updated and therefore must show the PDF since its code is inside:

"ValueEventListener listener = new ValueEventListener() { @Override
            public void onDataChange(DataSnapshot dataSnapshot) {"

This code is inside the "GetData" button.

What can I do?

Thanks:)

  public void boton3_basededato(View view){
    templatePDF.openDocument();
    templatePDF.addParagraph(shortText);
    templatePDF.addParagraph(longText);
    DatabaseReference mRootReference;
    mRootReference = FirebaseDatabase.getInstance().getReference();
    mRootReference.child("Usuario").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            int cont=0;
            for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                String todo=snapshot.getValue().toString();

                if(todo.contains("{")){cont++;}

            }
            int j=0;
            for (DataSnapshot snapshot : dataSnapshot.getChildren()) {  //lo que esta despues de los ":" obtiene los datos alfanumericos y el snapshot.getValue obtiene los demas valores que estan dentro

                userPojo user = snapshot.getValue(userPojo.class); // que me traiga los valores que solo estan declarados en el userPojo.class


                templatePDF.addParagraph(user.getCodigo());
                templatePDF.addParagraph(user.getPlano());
                templatePDF.addParagraph(user.getElemento());
                templatePDF.addParagraph(String.valueOf(user.getSlump()));
                templatePDF.addParagraph(user.getComentario());
                templatePDF.addParagraph(espacios);

                j++;
                if(j==cont){
                   templatePDF.closeDocument();
                   templatePDF.viewBasededatos();
                }

        }
        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });
}
    
asked by Pedro N. 06.11.2018 в 00:17
source

1 answer

1

Change this addValueEventListener for this addListenerForSingleValueEvent in your method boton3_basededato

mRootReference.child("Usuario").addListenerForSingleValueEvent...

addValueEventListener

This means that you are always listening to changes under your reference .child("Usuario") , so if you change anything in it the boton3_basededato method will be executed. What happens is that when you press the button to generate the PDF, you attach a listener to that reference, and for each script to that reference your listener is being launched that generates the PDF.

addListenerForSingleValueEvent

It is only going to be launched the moment you press the button and then it will stop listening to that reference for changes, so that the request will be executed only once. In this way, when you press to generate the PDF you will generate it once with the data inside the reference and then it will not do it again if you upload data unless you press the button again to obtain data from that reference.

    
answered by 06.11.2018 / 18:01
source