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.
每当我点击更改状态按钮时,它会改变状态,但由于它在onDataChange中,它会导致无限循环...无时间改变其状态...如何摆脱这个?如何走出这个循环?我想只改变一次状态。
1 个解决方案
#1
0
change
更改
addValueEventListener
addValueEventListener
to this
对此
addListenerForSingleValueEvent
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) {
}
});
#1
0
change
更改
addValueEventListener
addValueEventListener
to this
对此
addListenerForSingleValueEvent
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) {
}
});