Question: I have a code something like this:

```
change_status_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mfirebasedatabase.getReference().child("genre_items").child(genre_status_selected).child(genreitem_status_selected).addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        if(genre_status_selected!="" && genreitem_status_selected!="" && dataSnapshot.child("status").getValue().equals("available"))
                        {
                            mfirebasedatabase.getReference().child("genre_items").child(genre_status_selected).child(genreitem_status_selected).child("status").setValue("unavailable");
                                                        }
                        else if(genre_status_selected!="" && genreitem_status_selected!="" && dataSnapshot.child("status").getValue().equals("unavailable"))
                        {
                            mfirebasedatabase.getReference().child("genre_items").child(genre_status_selected).child(genreitem_status_selected).child("status").setValue("available");

                        }
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });
            }
        });

```

whenever I click the change status button , it changes the status ,but since it is in the onDataChange it leads to an infinite loop...changing its state infinite no of times...how to get rid of this? how to get out of this loop? I want to change its status once only.

Answer: 
change 

> 
> addValueEventListener
> 
> 
> 

to this

> 
> addListenerForSingleValueEvent
> 
> 
> 

```
mfirebasedatabase.getReference().child("genre_items").child(genre_status_selected).child(genreitem_status_selected).addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        if(genre_status_selected!="" && genreitem_status_selected!="" && dataSnapshot.child("status").getValue().equals("available"))
                        {
                            mfirebasedatabase.getReference().child("genre_items").child(genre_status_selected).child(genreitem_status_selected).child("status").setValue("unavailable");
                                                        }
                        else if(genre_status_selected!="" && genreitem_status_selected!="" && dataSnapshot.child("status").getValue().equals("unavailable"))
                        {
                            mfirebasedatabase.getReference().child("genre_items").child(genre_status_selected).child(genreitem_status_selected).child("status").setValue("available");

                        }
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });

```
Use addListenerForSingleValueEvent()

example

```html
database.getReference().child("chats").child(senderRoom).child("messages").addListenerForSingleValueEvent(new ValueEventListener() {
                    @SuppressLint("NotifyDataSetChanged")
                    @Override
                    public void onDataChange(@NonNull DataSnapshot snapshot) {
                            database.getReference("chats/"+senderRoom+"/messages").removeValue();
                            Toast.makeText(ChattingActivity.this, "All chats delete", Toast.LENGTH_LONG).show();
                    }

                    @Override
                    public void onCancelled(@NonNull DatabaseError error) {
                        Toast.makeText(ChattingActivity.this, "Error! Chats not delete", Toast.LENGTH_LONG).show();
                    }
                });
```