持久性Firebase数据库

时间:2017-02-03 18:09:58

标签: android firebase firebase-realtime-database

我正在制作投票应用。有两个按钮,点击次数存储在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() { }

}

由于

1 个答案:

答案 0 :(得分:1)

Counter和counter1是局部变量,您不会持久化它们。当您将counter和counter1的值存储到数据库时,您应该只检索该值并将其递增1。

mRefChild.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            // you can retrieve your stored value from dataSnapshot 
            Object data = dataSnapshot.getValue()
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

听起来您正在使用Firebase的订购版本。请参阅此链接https://firebase.google.com/support/guides/firebase-android并升级您的项目。

上面的链接还提供了有关读取数据的详细信息。在您的情况下,addListenerForSingleValueEvent(..)将解决您的问题。

https://www.firebase.com/docs/java-api/javadoc/com/firebase/client/DataSnapshot.html

从你的代码

听起来你直接存储投票的价值,没有表结构或任何东西。所以dataSnapshot.getValue()应该返回你的值。但是,我建议您调试代码并放置一个断点Object data = dataSnapshot.getValue()并分析dataSnapshot实例中的信息。它就在那里。

检索值后,将其分配给counter或counter1变量。因此,每次启动应用程序时,您都会将更新后的值更新回您的变量。你已经在按钮点击上做了反问题,所以它应该按原样运行。