Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.iqbal.homesecurity;
- import android.os.Bundle;
- import android.support.v4.app.Fragment;
- import android.util.Log;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- 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;
- /**
- * A simple {@link Fragment} subclass.
- */
- public class DashboardFragment extends Fragment {
- FirebaseDatabase database = FirebaseDatabase.getInstance();
- DatabaseReference myRef = database.getReference();
- private TextView textAlarmStatus;
- public DashboardFragment() {
- // Required empty public constructor
- }
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- // Inflate the layout for this fragment
- View view = inflater.inflate(R.layout.fragment_dashboard, container, false);
- // Read from the database
- myRef.child("alarm_status").addValueEventListener(new ValueEventListener() {
- @Override
- public void onDataChange(DataSnapshot dataSnapshot) {
- // This method is called once with the initial value and again
- // whenever data at this location is updated.
- String value = dataSnapshot.getValue(String.class);
- //contoh yg aku ditampilin di text view
- textAlarmStatus = getView().findViewById(R.id.txt_alarm_status);
- textAlarmStatus.setText(value);
- }
- @Override
- public void onCancelled(DatabaseError error) {
- // Failed to read value
- Log.w("ERROR", "Failed to read value.", error.toException());
- }
- });
- return view;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement