热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

每当我重新启动Android应用程序时,Firebase中的投票计数都会重置

我正在创建一个投票应用程序.Firebase数据库计算按钮的点击次数并显示它.但是,在关闭应用程序并重新启动时,点击次数从零开始.即使关闭并重新启动应用程序后,如何仍将投票数添加到

我正在创建一个投票应用程序. Firebase数据库计算按钮的点击次数并显示它.但是,在关闭应用程序并重新启动时,点击次数从零开始.即使关闭并重新启动应用程序后,如何仍将投票数添加到子节点,而不是从零开始投票?

mainactivity.java

public class Main2Activity extends AppCompatActivity {
private Firebase mRootRef;
private Button mBtn1;
private Button mBtn2;
int counter = 0;
int counter1 = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Firebase.setAndroidContext(this);
mBtn1 = (Button) findViewById(R.id.btn1);
mBtn2 = (Button) findViewById(R.id.btn2);
mBtn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (v.equals(mBtn1)) {
mRootRef = new Firebase("https://voting-cf0fa.firebaseio.com/House/Jupiter/Player 1");
Firebase mRefChild = mRootRef.child("Votes");
counter++;
mRefChild.setValue(counter);
MediaPlayer click1 =MediaPlayer.create(getApplicationContext(), R.raw.click);
click1.start();
mBtn1.setEnabled(false);
mBtn2.setEnabled(false);
Timer buttOnTimer= new Timer();
buttonTimer.schedule(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
mBtn1.setEnabled(true);
mBtn2.setEnabled(true);
}
});
}
}, 5000);
final AlertDialog.Builder Voted = new AlertDialog.Builder(Main2Activity.this);
Voted.setTitle("Voted");
Voted.setMessage("You have cast Your Vote!");
Voted.setCancelable(false);
final AlertDialog dlg = Voted.create();
dlg.show();
final Timer t = new Timer();
t.schedule(new TimerTask() {
@Override
public void run() {
dlg.dismiss();
t.cancel();
}
}, 5000);
}
}
});
mBtn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mRootRef = new Firebase("https://voting-cf0fa.firebaseio.com/House/Jupiter/Player 2");
if (v.equals(mBtn2)) {
Firebase mRefChild = mRootRef.child("Votes");
counter1++;
mRefChild.setValue(counter1);
MediaPlayer click2 =MediaPlayer.create(getApplicationContext(), R.raw.click);
click2.start();
mBtn2.setEnabled(false);
mBtn1.setEnabled(false);
Timer buttOnTimer= new Timer();
buttonTimer.schedule(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
mBtn2.setEnabled(true);
mBtn1.setEnabled(true);
}
});
}
}, 5000);
final AlertDialog.Builder Voted = new AlertDialog.Builder(Main2Activity.this);
Voted.setTitle("Voted");
Voted.setMessage("You Have cast your Vote");
Voted.setCancelable(false);
final AlertDialog dlg = Voted.create();
dlg.show();
final Timer t = new Timer();
t.schedule(new TimerTask() {
@Override
public void run() {
dlg.dismiss();
t.cancel();
}
}, 5000);
}
}
});
}
@Override
public void onBackPressed() { }
}

解决方法:

从Firebase文档中:

处理可能被并发更新破坏的复杂数据时,请使用我们的交易功能

public void incrementCounter() {
firebase.runTransaction(new Transaction.Handler() {
@Override
public Transaction.Result doTransaction(final MutableData currentData) {
if (currentData.getValue() == null) {
currentData.setValue(1);
} else {
currentData.setValue((Long) currentData.getValue() + 1);
}
return Transaction.success(currentData);
}
@Override
public void onComplete(FirebaseError firebaseError, boolean committed, DataSnapshot currentData) {
if (firebaseError != null) {
Log.d("Firebase counter increment failed.");
} else {
Log.d("Firebase counter increment succeeded.");
}
}
});
}


推荐阅读
author-avatar
手机用户2502914287
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有