Firebase-Realtime Database on Android that can be used with copy

Introduction

I think most people who come into contact with Firebase want to use "** Realtime Database **". (Rogue)

However, the only articles that come out after researching are ~~ off-target ~~. I think that it will be necessary to verify to some extent in order to use it properly. Of course, verification is very important, and I understand that it is the real thrill.

However, there are people (including myself, of course) who are likely to rampage the inner beast, saying "** I want to use it for the time being! **".

For those people, I'll put together the code so that you can copy and paste it as much as possible.

** "I don't understand this, too" "Copy and paste is s (omitted" for learning ... etc. It will be very embarrassing for people, so please browser back. **

Data storage

Saving single data

I don't think I will use it first For example, if you want to save only the character string "** Hello World !**", write it like this.

FirebaseDatabase database = FirebaseDatabase.getInstance(); 
DatabaseReference myRef = database.getReference(); 
myRef.setValue("Hello World !"); 

Save child data

For example, if you want to save the character string "** apple " in the saved data " Items **", write it like this.

FirebaseDatabase database = FirebaseDatabase.getInstance(); 
DatabaseReference myRef = database.getReference("Items");
myRef.setValue("apple"); 

This can be included as much as you want using . Child , For example, if you want to save the character string "" sweet "" in the character string "** apple " in the saved data " Items **", write it like this.

FirebaseDatabase database = FirebaseDatabase.getInstance(); 
DatabaseReference myRef = database.getReference("Items").child("apple"); 
myRef.setValue("sweet"); 

In other words, you can also do this.

FirebaseDatabase database = FirebaseDatabase.getInstance(); 
DatabaseReference myRef = database.getReference("Items").child("1").child("2").child("3").child("4").child("5").child("6").child("7").child("8").child("9").child("10");
myRef.setValue("good job"); 

Because Firebase is excellent, if there is no parent data, it will create it as well.

Save with custom model

You can save the data held by your own model as it is. It's very easy to write ** (or rather, as it is), just put it in the same way.

Item item=new Item();
item.setName("apple");
item.setTaste("sweet");
item.setPrice(500);

FirebaseDatabase database = FirebaseDatabase.getInstance(); 
DatabaseReference myRef = database.getReference("Items").child("apple"); 
myRef.setValue(item); 

This will save 3 data in "** apple " of " Items **".

Data acquisition

"** The real thrill of Realtime Database is that it is synchronized in real time! **" ~~ Everyone wants to do this, so they're doing Firebase. ~~ rest assured. Firebase is also made on the premise of this, so conversely, you can not get data unless you use this.

Get basic type data

For example, if you want to get the data when the data in the parent data "** Items **" is changed, write like this.


FirebaseDatabase database = FirebaseDatabase.getInstance();
        DatabaseReference userRef = database.getReference("Items");
        userRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                //Get an instance
                for (DataSnapshot data : dataSnapshot.getChildren()) {
                    Log.d("",data.getValue().toString);
                }
            }

            @Override
            public void onCancelled(DatabaseError error) {
                //Failed to get data
            }
        });

If "** apple ", " pen " and " gorilla " are saved in " Items **", then ** size of ** dataSnapshot.getChildren () ** ** becomes 3 apple pen gorilla Should be logged.

Get custom model data

For example, if the above custom model was saved, write it like this.


FirebaseDatabase database = FirebaseDatabase.getInstance();
        DatabaseReference userRef = database.getReference("Items");
        userRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                //Get an instance
                for (DataSnapshot data : dataSnapshot.getChildren()) {
           Item item=data.getValue(Item.class);
                }
            }

            @Override
            public void onCancelled(DatabaseError error) {
                //Failed to get data
            }
        });

Recommended Posts

Firebase-Realtime Database on Android that can be used with copy
Organize methods that can be used with StringUtils
[Ruby] Methods that can be used with strings
Simple slot machine implementation that can be used with copy and paste
Summary of css selectors that can be used with Nookogiri
Four-in-a-row with gravity that can be played on the console
Static analysis tool that can be used on GitHub [Java version]
SwiftUI View that can be used in combination with other frameworks
[Rails] "pry-rails" that can be used when saving with the create method
[Android Studio] Description that can be continuously input in SQLite Database [Java]
Performance analysis and failure diagnostic tools that can be used with OpenJDK
Ruby array methods that can be used with Rails (other than each)
[Swift] Color Picker that can be used with copy and paste (palette that allows you to freely select colors)
You can do it with copy! Aspect-oriented programming (Android)
Range where variables can be used with ruby [Scope]
About the matter that hidden_field can be used insanely
Convenient shortcut keys that can be used in Eclipse
Ruby on Rails 5 quick learning practice guide that can be used in the field Summary
Syntax and exception occurrence conditions that can be used when comparing with null in Java
The world of Azure IoT that can be played on the DE10-Nano board: Ajuchika with FPGA !!?
Learning Ruby with AtCoder Beginners Selection [Some Sums] Increase the methods that can be used
Build an environment where pip3 can be used with CentOS7 + Python3
Summary of ORM "uroboroSQL" that can be used in enterprise Java
File form status check sheet that can be deleted with thumbnails
I made a question that can be used for a technical interview
Power skills that can be used quickly at any time --Reflection
Summary of JDK that can be installed with Homebrew (as of November 2019)
Introduction to Java that can be understood even with Krillin (Part 1)
Event handling with RxBus on Android
Java file input / output processing that can be used through historical background
About the problem that the server can not be started with rails s
Set the access load that can be changed graphically with JMeter (Part 1)
Library summary that seems to be often used in recent Android development (2019/11)
A memo that enabled VS Code + JUnit 5 to be used on Windows 10
[ERROR message display] A simplified version that can be used at any time with the rails partial template.
Initial settings until S2Dao can be used
Object-oriented that can be understood by fairies
Technology excerpt that can be used for creating EC sites in Java training
I made a THETA API client that can be used for plug-in development
Check with Java / Kotlin that files cannot be written in UAC on Windows
A ruby ​​script that creates an rsa private key that can be used with OpenSSL from any two prime numbers