Getting Started with the Firebase Realtime Database on Android - Firecasts

Getting Started with the Firebase Realtime Database on Android - Firecasts - Hello friends Android tutorial and tips trik, The article you are reading this time with the title Getting Started with the Firebase Realtime Database on Android - Firecasts, we have prepared this article for you to read and download the information therein. hopefully fill posts article -, article android, article Database, article Firebase, article Firecasts, article Getting, article on, article Realtime, article Started, article the, article Tutorial, article with, we can write this you understand. Well, happy reading.

Title : Getting Started with the Firebase Realtime Database on Android - Firecasts
link : Getting Started with the Firebase Realtime Database on Android - Firecasts

Baca juga


Getting Started with the Firebase Realtime Database on Android - Firecasts

[MUSIC PLAYING] What's up, everyone? David here fromthe Firebase team.

And welcome to a brandnew season of Firecasts where we're going to be showingyou the all-new Firebase.

And on today'sepisode, I'm going to show you how to get set upwith the real-time database in Android.

And since that's soeasy, we'll spend the rest of our timecovering the basics of the real-time databaseby building an app.

Then you'll knoweverything that you need to know to get startedin just a few minutes.

So let's go and dive in.

Open your Gradle file.

And the first thingyou need to do is include Google Play Services.

So in Dependencies,I'm just going to set the class path forGoogle Play Services 3.

0.

And then I need to open upthe app build.

Gradle file.

In the Dependenciessection, I need to include the Firebasecore and real-time database dependencies.

And these are justcom.

Google.

Firebase and firebase-databashand firebase-core.

And lastly, I need to applythe plugin for Google Play Services.

So there you go.

It's as easy as that.

Now that we're all setup, let's build an app.

We're going to build a social,local, mobile, crowdsourced weather app that will tell youwhether it's sunny or foggy in San Francisco.

It is a pretty basicapp, but it covers the fundamentals ofsaving and synchronizing data in real time.

So let me go and showyou how that works.

So now that I'm all set up,I'm going to build out my UI.

So I'm going to open upthe Main Activity layout.

So I'll delete thestock text view.

And I'm going to add a largertext view to the screen.

And I'm going to setthe text to Condition and then change the IDto Text View Condition.

Now I need to add thebuttons for sunny and foggy.

So I'll add a button down here.

I'll change the textto Sunny and the button ID to buttonsunny, andthen drag another one and just do the same for foggy.

So the text is Foggy andthe ID is buttonfoggy.

So, great.

The UI's all set up.

But we actually need towire these components in the Main Activity.

So I'll open upthe Main Activity.

And then I will addthem as properties, and then just useFind View by ID to wire them up overhere in onCreate.

So now we need to talk tothe real-time database.

The first thing Ineed is a connection.

And to do that, I'm going tocreate a database reference.

So as a property, I'll typedatabase reference is m root reference.

And that's going toequal Firebasedatabase.

Getinstance.

Getreference.

And the reason why I'mcalling this root ref is because, whenwe get a reference, it gets us a reference to theroot of the Firebase JSON tree.

So the next step is tolisten for real-time changes.

Whenever the conditionin the database changes, we want to update thetext view's text property.

So to listen forthat change, I'm going to create a value listenerin the onStart Life Cycle method.

So create onStart.

And then on theside of onStart I'm going to create achild reference.

I'm going to call itthe Condition reference.

And that is going to equalthe root reference I'm going to call.

Child and then passthrough the string condition.

So by calling childon the root, we're creating a location ofcondition underneath the roots.

And then we're goingto be setting a value, so either, you know,sunny or foggy.

So I'll just copy it uphere and then paste it below the root refand then change it so it's m condition ref.

So now that I have accessto the condition location, I'll create a listener.

So instead of onStart, I'm goingto call the Add Value Event Listener method.

And notice that I'mcalling Add Value Event Listener on the ConditionRef.

So we're attaching it tothe location of Condition.

Inside of here, I'm goingto give it a new value event listener.

And this is justtwo methods inside of the anonymous inner class.

So the first is onDataChange.

And this will get fired everysingle time the condition value updates in thereal-time database.

And then onCanceled is ifwe run into any errors.

So inside onDataChange,we're getting our data back as a data snapshot.

And this contains your dataand other useful methods.

So what I want to do is Iwant to use this snapshot and synchronize itto the text view.

So first I'm going to getthe data back as a string.

So I'll calldataSnapshot.

GetValue.

And then, to makesure it's a string, I'm going to passin the String Class.

And then now that Iknow it's a string, I can go to the text viewand then call Set Text by passing in the text.

So I have the app upand running over here.

And I have the real-timedatabase viewer right here.

So there's no data yet.

So let me add acondition location.

So I'll hit plus,type Condition.

And we'll set its initial valueto sunny with an exclamation mark.

Nothing happened.

Why is that? Well, that's because thedefault security rules only allow authenticatedusers to access data.

So if I click overto the Rules tab, you can see thatwithin these rules.

We have.

Read and.

Write.

And they both have a ruleof off not equal to null.

And like I said, this onlyallows authenticated users to write or read fromanywhere in your database.

So we're just developing an app.

So we don't have to lockdown our rules just yet.

So we're going to set everythingto be readable and writeable.

But when you goto production, you should not do that becauseanyone can access your data.

And I think you know that'sprobably not a good thing.

So for Read I'm goingto just write true and the same for Write.

And then just make sureto publish your changes.

So now back in the Data tab,I'm going to restart the app.

And you can see that wehave the value of Sunny.

But to prove it toyou, I'll actually go and change the value.

So we'll remove theexclamation mark.

And, boom, itupdates in real time.

So that works.

But if we are to clickon any of these buttons, it's not going toupdate this condition and therefore updatethe text view.

So let's change that in our app.

So back in AndroidStudio, I need to set Click Listenersfor these two buttons.

So on m buttonsunny,I'll set onClickListener with a new onClickListener.

And then inside ofthe Click Listener, I'm going to callConditionRef.

Setvalue.

So keep in mind that thecondition ref connects this out to the slash condition location.

So if we callsetvalue on it it, it will replace anythingthat's currently there, which in this case is sunny.

So whenever someone clickson the Sunny button, we want the value to be sunny.

So I'll throw ina string of Sunny.

And then I'm just going tocopy and do the same for foggy.

So it's really important tounderstand the data flow here.

It's that we're actually callingsetvalue in both of these click listeners.

And we're not directlyupdating the text view.

We're not directly updatingsome internal data source.

We're updating thereal-time database.

And then when thereal-time database updates, the onDataChange methodfires and, therefore, updates our text view.

So the lesson here is don'tmodify your local state.

Let everything come fromthe real-time listeners.

So now when I click thebuttons, the database updates in real timeand therefore, so does the text view.

And there you go.

You're all set up with thereal-time database and only in a couple of minutes.

And that's allfor this Firecast.

But please leave your questionsin the comments below or reach out to us on Twitter and G+with the hashtag #AskFirebase.

And we're going to be droppinga new Firecast every few days, so don't forget to hit thatSubscribe button to stay up to date.

I'm David East, andthanks for watching.

[MUSIC PLAYING].



Thus Article Getting Started with the Firebase Realtime Database on Android - Firecasts

That article Getting Started with the Firebase Realtime Database on Android - Firecasts This time, hopefully we can give you all of the benefits. Well, I'll see you in another article post.

You now read the article Getting Started with the Firebase Realtime Database on Android - Firecasts the link address http://svedda79.blogspot.com/2016/02/getting-started-with-firebase-realtime.html

0 Response to "Getting Started with the Firebase Realtime Database on Android - Firecasts"

Post a Comment