java Firebase数据库读/写示例

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java Firebase数据库读/写示例相关的知识,希望对你有一定的参考价值。

package com.zeromolecule.firebaseexperiment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import java.util.Random;

/**
 * Created by David Macan, 2017
 */

public class TestActivity extends AppCompatActivity {

    private FirebaseDatabase database;
    private DatabaseReference notesDB;

    private TextView txtMessage;
    private Button btnNew;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initialize();
        listenData();
        btnNew.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                writeData();
            }
        });
    }

    private void initialize() {
        database = FirebaseDatabase.getInstance();
        notesDB = database.getReference("notes");
        txtMessage = (TextView) findViewById(R.id.txtMessage);
        btnNew = (Button) findViewById(R.id.btnNew);
    }

    private void listenData() {
        notesDB.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                // Ovdje radiš nešto s rezultatima
                Iterable<DataSnapshot> result = dataSnapshot.getChildren();
                String text = dataSnapshot.toString();
                txtMessage.setText(text);
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                // Ovdje hendlaš error
                Toast.makeText(getBaseContext(), "Oops, something went wrong", Toast.LENGTH_SHORT)
                        .show();
                Log.e("DAM", "oops, something went wrong", databaseError.toException());
            }
        });
    }

    private void writeData() {
        final int number = new Random().nextInt(100);
        Note note = new Note(
                "Title" + number,
                "Description" + number
        );
        notesDB.child("notes").child("Note" + number)
                .setValue(note)
                .addOnCompleteListener(new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> task) {
                        Toast.makeText(getBaseContext(), "Created new note with number " + number, Toast.LENGTH_SHORT)
                                .show();
                    }
                });
    }
}

以上是关于java Firebase数据库读/写示例的主要内容,如果未能解决你的问题,请参考以下文章

Firebase 存储读/写带宽问题

将 Firebase 读/写权限限制为只有我自己

使用 Flutter 的 Firebase 读/写规则不起作用?

Fire Storage Exception([firebase_storage/unauthorized] 用户无权执行所需的操作。)(我的规则是允许读/写)

深入理解Java内存模型——重排序

深入理解Java内存模型——重排序