Last active
October 12, 2016 13:32
-
-
Save khatthaphone/10894fdaa54a512959c4bf5d3a8e5916 to your computer and use it in GitHub Desktop.
[Blog post] ລອງຫຼິ້ນ Firebase Realtime Database ກັບ Android
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dependencies { | |
// version ອາດມີການປ່ຽນແປງໃນອະນາຄົດ | |
compile 'com.google.firebase:firebase-core:9.4.0' | |
compile 'com.google.firebase:firebase-database:9.4.0' | |
} | |
// ເພີ່ມໃສ່ລຸ່ມສຸດຂອງໄຟລ໌ | |
apply 'com.google.gms:google-services' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
buildscript { | |
dependencies { | |
classpath 'com.google.gms:google-services:3.0.0' | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:paddingBottom="@dimen/activity_vertical_margin" | |
android:paddingLeft="@dimen/activity_horizontal_margin" | |
android:paddingRight="@dimen/activity_horizontal_margin" | |
android:paddingTop="@dimen/activity_vertical_margin" | |
tools:context="com.khatthaphone.firebasedatabase.MainActivity"> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:id="@+id/condition" | |
android:text="ສະພາບອາກາດ" /> | |
</RelativeLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"rules": { | |
".read": true, | |
".write": "auth != null" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// your package name here / ໃສ່ຊື່ package ຂອງເຈົ້າເອງ | |
import android.os.Bundle; | |
import android.support.v7.app.AppCompatActivity; | |
import android.widget.TextView; | |
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; | |
public class MainActivity extends AppCompatActivity { | |
TextView textView; | |
DatabaseReference mDatabaseRef = FirebaseDatabase.getInstance().getReference(); | |
DatabaseReference mConditionRef = mDatabaseRef.child("condition"); | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
textView = (TextView) findViewById(R.id.condition); | |
} | |
@Override | |
protected void onStart() { | |
super.onStart(); | |
mConditionRef.addValueEventListener(new ValueEventListener() { | |
@Override | |
public void onDataChange(DataSnapshot dataSnapshot) { | |
String text = dataSnapshot.getValue(String.class); | |
textView.setText(text); | |
} | |
@Override | |
public void onCancelled(DatabaseError databaseError) { | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment