我的代码检查数据库中的值是否等于扫描条形码的值,一切正常,但Toast或SetValue不断发生,直到Qrcode上的摄像头运行ValueEventListener的另一个活动,我无法运行,所以我不知道如何只需执行一次此代码。
这是我执行此过程的代码
// Called when a QR is decoded
// "text" : the text encoded in QR
// "points" : points where QR control points are placed
@Override
public void onQRCodeRead(String text, PointF[] points) {
pointsOverlayView.setPoints(points);
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference coinsUidRef = rootRef.child("coinsUid").child(text);
ValueEventListener valueEventListener = new ValueEventListener() {
@SuppressWarnings("ConstantConditions")
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
Log.v(text,""+dataSnapshot);
if (!dataSnapshot.exists()) {
Toast toast = Toast.makeText(getApplicationContext(),
"Ошибка считвания! Считайте снова", Toast.LENGTH_SHORT);
toast.show();
} else {
long qrCoinsAmount = dataSnapshot.getValue(Long.class);
DatabaseReference coinsAmountRef = rootRef.child("users").child(getUid()).child("coinsAmount");
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
long coinsAmount = dataSnapshot.getValue(Long.class);
coinsAmountRef.getRef().setValue(coinsAmount + qrCoinsAmount);
coinsUidRef.removeValue();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
};
coinsAmountRef.addListenerForSingleValueEvent(valueEventListener);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
};
coinsUidRef.addListenerForSingleValueEvent(valueEventListener);
}
我的日志
V /http://erwe: DataSnapshot { key = erwe, value = null }
V/http://erwe: DataSnapshot { key = erwe, value = null }
I/Toast: Show toast from OpPackageName:com.example.ecohelp, PackageName:com.example.ecohelp
V/http://erwe: DataSnapshot { key = erwe, value = null }
V/http://erwe: DataSnapshot { key = erwe, value = null }
I/Toast: Show toast from OpPackageName:com.example.ecohelp, PackageName:com.example.ecohelp
V/http://erwe: DataSnapshot { key = erwe, value = null }
V/http://erwe: DataSnapshot { key = erwe, value = null }
I/Toast: Show toast from OpPackageName:com.example.ecohelp, PackageName:com.example.ecohelp
I/Toast: Show toast from OpPackageName:com.example.ecohelp, PackageName:com.example.ecohelpV/http://erwe: DataSnapshot { key = erwe, value = null }
V/http://erwe: DataSnapshot { key = erwe, value = null }
I/Toast: Show toast from OpPackageName:com.example.ecohelp, PackageName:com.example.ecohelp
V/http://erwe: DataSnapshot { key = erwe, value = null }
V/http://erwe: DataSnapshot { key = erwe, value = null }
I/Toast: Show toast from OpPackageName:com.example.ecohelp, PackageName:com.example.ecohelp
V/http://erwe: DataSnapshot { key = erwe, value = null }
V/http://erwe: DataSnapshot { key = erwe, value = null }
I/Toast: Show toast from OpPackageName:com.example.ecohelp, PackageName:com.example.ecohelp
I/Toast: Show toast from OpPackageName:com.example.ecohelp, PackageName:com.example.ecohelp
P.S 我只是从数据库中删除了该值,现在数据库中的值现在仅更改了一次(根据需要),但是Toast仍然无休止地出现
答案 0 :(得分:1)
尝试删除onDataChange中的值监听器
ref.removeEventListener(this);
因此从DataChange回调的两个引用中删除事件侦听器。看看这个答案