How to make a like function in Firebase?











up vote
0
down vote

favorite












There is an application in it posts. Likes, done as follows:



postReference = firebaseDatabase.getInstance().getReference().child("Posts");   
holder.likeBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final DatabaseReference likeReference = postReference.child(listUserId).child("Likes");
likeReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (!dataSnapshot.hasChild(currentUserId)) {
likeReference.child(currentUserId).removeValue();
} else {
likeReference.child(currentUserId).setValue("Like");
}
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}


The problem is that after pressing the button, the record in the database begins to be added and deleted without stopping. How to fix it? My DB:



my_database










share|improve this question




























    up vote
    0
    down vote

    favorite












    There is an application in it posts. Likes, done as follows:



    postReference = firebaseDatabase.getInstance().getReference().child("Posts");   
    holder.likeBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    final DatabaseReference likeReference = postReference.child(listUserId).child("Likes");
    likeReference.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
    if (!dataSnapshot.hasChild(currentUserId)) {
    likeReference.child(currentUserId).removeValue();
    } else {
    likeReference.child(currentUserId).setValue("Like");
    }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
    }
    });
    }
    }


    The problem is that after pressing the button, the record in the database begins to be added and deleted without stopping. How to fix it? My DB:



    my_database










    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      There is an application in it posts. Likes, done as follows:



      postReference = firebaseDatabase.getInstance().getReference().child("Posts");   
      holder.likeBtn.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
      final DatabaseReference likeReference = postReference.child(listUserId).child("Likes");
      likeReference.addValueEventListener(new ValueEventListener() {
      @Override
      public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
      if (!dataSnapshot.hasChild(currentUserId)) {
      likeReference.child(currentUserId).removeValue();
      } else {
      likeReference.child(currentUserId).setValue("Like");
      }
      }

      @Override
      public void onCancelled(@NonNull DatabaseError databaseError) {
      }
      });
      }
      }


      The problem is that after pressing the button, the record in the database begins to be added and deleted without stopping. How to fix it? My DB:



      my_database










      share|improve this question















      There is an application in it posts. Likes, done as follows:



      postReference = firebaseDatabase.getInstance().getReference().child("Posts");   
      holder.likeBtn.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
      final DatabaseReference likeReference = postReference.child(listUserId).child("Likes");
      likeReference.addValueEventListener(new ValueEventListener() {
      @Override
      public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
      if (!dataSnapshot.hasChild(currentUserId)) {
      likeReference.child(currentUserId).removeValue();
      } else {
      likeReference.child(currentUserId).setValue("Like");
      }
      }

      @Override
      public void onCancelled(@NonNull DatabaseError databaseError) {
      }
      });
      }
      }


      The problem is that after pressing the button, the record in the database begins to be added and deleted without stopping. How to fix it? My DB:



      my_database







      java android firebase firebase-realtime-database






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 10 at 9:55









      Alex Mamo

      38.6k72758




      38.6k72758










      asked Nov 10 at 9:29









      Binary

      51




      51
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          To solve this, please change the following line of code:



          likeReference.addValueEventListener(new ValueEventListener() {}


          to



          likeReference.addListenerForSingleValueEvent(new ValueEventListener() {}


          See addListenerForSingleValueEvent official documentation:




          Add a listener for a single change in the data at this location.




          Using addValueEventListener it means that you are keeping the listener active all the time. To remove the listener, please see my answer from this post.






          share|improve this answer





















          • Thank a lot, everything works fine.
            – Binary
            Nov 10 at 15:10











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53237624%2fhow-to-make-a-like-function-in-firebase%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote



          accepted










          To solve this, please change the following line of code:



          likeReference.addValueEventListener(new ValueEventListener() {}


          to



          likeReference.addListenerForSingleValueEvent(new ValueEventListener() {}


          See addListenerForSingleValueEvent official documentation:




          Add a listener for a single change in the data at this location.




          Using addValueEventListener it means that you are keeping the listener active all the time. To remove the listener, please see my answer from this post.






          share|improve this answer





















          • Thank a lot, everything works fine.
            – Binary
            Nov 10 at 15:10















          up vote
          0
          down vote



          accepted










          To solve this, please change the following line of code:



          likeReference.addValueEventListener(new ValueEventListener() {}


          to



          likeReference.addListenerForSingleValueEvent(new ValueEventListener() {}


          See addListenerForSingleValueEvent official documentation:




          Add a listener for a single change in the data at this location.




          Using addValueEventListener it means that you are keeping the listener active all the time. To remove the listener, please see my answer from this post.






          share|improve this answer





















          • Thank a lot, everything works fine.
            – Binary
            Nov 10 at 15:10













          up vote
          0
          down vote



          accepted







          up vote
          0
          down vote



          accepted






          To solve this, please change the following line of code:



          likeReference.addValueEventListener(new ValueEventListener() {}


          to



          likeReference.addListenerForSingleValueEvent(new ValueEventListener() {}


          See addListenerForSingleValueEvent official documentation:




          Add a listener for a single change in the data at this location.




          Using addValueEventListener it means that you are keeping the listener active all the time. To remove the listener, please see my answer from this post.






          share|improve this answer












          To solve this, please change the following line of code:



          likeReference.addValueEventListener(new ValueEventListener() {}


          to



          likeReference.addListenerForSingleValueEvent(new ValueEventListener() {}


          See addListenerForSingleValueEvent official documentation:




          Add a listener for a single change in the data at this location.




          Using addValueEventListener it means that you are keeping the listener active all the time. To remove the listener, please see my answer from this post.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 10 at 9:50









          Alex Mamo

          38.6k72758




          38.6k72758












          • Thank a lot, everything works fine.
            – Binary
            Nov 10 at 15:10


















          • Thank a lot, everything works fine.
            – Binary
            Nov 10 at 15:10
















          Thank a lot, everything works fine.
          – Binary
          Nov 10 at 15:10




          Thank a lot, everything works fine.
          – Binary
          Nov 10 at 15:10


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53237624%2fhow-to-make-a-like-function-in-firebase%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          Xamarin.form Move up view when keyboard appear

          Post-Redirect-Get with Spring WebFlux and Thymeleaf

          Anylogic : not able to use stopDelay()