我正在设置一个更新功能,每次用户单击按钮时,firestore中的字段都会递减1。.....但是问题是,它不仅会持续递减直到值达到零为止>
final String di=getIntent().getStringExtra("DocumentID");
DocumentReference dref=db.collection("User").document(di);
Toast.makeText(managersAction.this," "+di+" "+j,Toast.LENGTH_LONG).show();
dref.addSnapshotListener(new EventListener<DocumentSnapshot>() {
@Override
public void onEvent(
@Nullable DocumentSnapshot documentSnapshot,
@Nullable F irebaseFirestoreException e) {
String space=documentSnapshot.getString("Space");
int s=Integer.parseInt(space);
if(s==0){
AlertDialog.Builder x=
new AlertDialog.Builder( managersAction.this);
x.setMessage("Not Allowed")
.setPositiveButton (android.R.string.ok,null);
AlertDialog v=x.create();
v.show();
}
else{
int newspace = s - 1;
final String sp = Integer.toString(newspace);
Toast.makeText(mnagersAction.this, " "+sp,Toast.LENGTH_LONG).show ();
DocumentReference dref=db.collection("User").document(di);
dref.update("Space",sp);
return;
}
return;
}
});
答案 0 :(得分:0)
从侦听器中删除以下代码:
{{1}}
每当数据库发生更改时,将作为事件侦听器被调用。