Skip to content

Instantly share code, notes, and snippets.

@khatthaphone
Last active October 12, 2016 13:32
Show Gist options
  • Save khatthaphone/10894fdaa54a512959c4bf5d3a8e5916 to your computer and use it in GitHub Desktop.
Save khatthaphone/10894fdaa54a512959c4bf5d3a8e5916 to your computer and use it in GitHub Desktop.
[Blog post] ລອງຫຼິ້ນ Firebase Realtime Database ກັບ Android
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'
buildscript {
dependencies {
classpath 'com.google.gms:google-services:3.0.0'
}
}
<?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>
{
"rules": {
".read": true,
".write": "auth != null"
}
}
// 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