简体   繁体   中英

Update value in Firebase Realtime Database (Offline) - Android

I need update one value which is the sum of executed tests in my Android application. I thought, that I can add each device and value of executed tests for this devices. Next I would like to get sum for all devices.

Now I have:

import android.content.Intent;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

public class MainActivity extends AppCompatActivity {

    private Button startTestButton;
    private FirebaseDatabase database;
    private DatabaseReference myRef;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setTitle("Test");


        Answers.getInstance().reset();


        database = FirebaseDatabase.getInstance();
        myRef = FirebaseDatabase.getInstance().getReference();
        myRef.child("test").child("devices").child(Settings.Secure.ANDROID_ID).setValue(1);


        startTestButton = findViewById(R.id.buttonStartTest);
        startTestButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openQuestion1Activity();
            }
        });
    }

    public void openQuestion1Activity(){
        Intent intent = new Intent(this, Question1.class);
        startActivity(intent);
    }
}

I need offline database and sync values when I have internet connection. How can I do this? Can I please for example?

FirebaseDatabase.getInstance().setPersistenceEnabled(true)放在应用程序中首次调用FirebaseDatabase.getInstance()之前。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM