Advertisement
rachmadi

MainActivity Pelancong1

Feb 13th, 2017
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.68 KB | None | 0 0
  1. package mdp.ac.id.pelancong;
  2.  
  3. import android.content.Intent;
  4. import android.drm.DrmManagerClient;
  5. import android.support.annotation.NonNull;
  6. import android.support.v7.app.AppCompatActivity;
  7. import android.os.Bundle;
  8. import android.support.v7.view.menu.ListMenuItemView;
  9. import android.util.Log;
  10. import android.view.Menu;
  11. import android.view.MenuInflater;
  12. import android.view.MenuItem;
  13. import android.view.View;
  14. import android.widget.GridView;
  15. import android.widget.ProgressBar;
  16. import android.widget.TextView;
  17.  
  18. import com.google.firebase.auth.FirebaseAuth;
  19. import com.google.firebase.auth.FirebaseUser;
  20. import com.google.firebase.database.ChildEventListener;
  21. import com.google.firebase.database.DataSnapshot;
  22. import com.google.firebase.database.DatabaseError;
  23. import com.google.firebase.database.DatabaseReference;
  24. import com.google.firebase.database.FirebaseDatabase;
  25. import com.google.firebase.database.ValueEventListener;
  26.  
  27. import java.util.ArrayList;
  28. import java.util.HashMap;
  29. import java.util.Iterator;
  30. import java.util.List;
  31.  
  32. public class MainActivity extends AppCompatActivity {
  33.  
  34. FirebaseAuth mAuth;
  35. DatabaseReference mRoot, mRef;
  36. private FirebaseAuth.AuthStateListener mAuthListener;
  37. String TAG = "MainActivity";
  38. GridView gvLocation;
  39. TextView tvNoLocation;
  40. ProgressBar progressBar;
  41.  
  42. @Override
  43. protected void onCreate(Bundle savedInstanceState) {
  44. super.onCreate(savedInstanceState);
  45. setContentView(R.layout.activity_main);
  46.  
  47. mRoot = FirebaseDatabase.getInstance().getReference();
  48. mRef = mRoot.child("locations");
  49.  
  50. progressBar = (ProgressBar) findViewById(R.id.progressBar);
  51. gvLocation = (GridView) findViewById(R.id.gvLocation);
  52. tvNoLocation = (TextView) findViewById(R.id.tvNoLocation);
  53.  
  54. mAuth = FirebaseAuth.getInstance();
  55.  
  56. mAuthListener = new FirebaseAuth.AuthStateListener() {
  57. @Override
  58. public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
  59. FirebaseUser user = firebaseAuth.getCurrentUser();
  60. if (user != null) {
  61. // User is signed in
  62. Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
  63. } else {
  64. // User is signed out
  65. Log.d(TAG, "onAuthStateChanged:signed_out");
  66. startActivity(new Intent(MainActivity.this, SignInActivity.class));
  67. finish();
  68. }
  69. // ...
  70. }
  71. };
  72.  
  73. progressBar.setVisibility(View.VISIBLE);
  74. fetchLocation();
  75. }
  76.  
  77. private void fetchLocation() {
  78.  
  79. mRef.addValueEventListener(new ValueEventListener() {
  80. List<Location> locationData = new ArrayList<>();
  81. @Override
  82. public void onDataChange(DataSnapshot dataSnapshot) {
  83. Iterator<DataSnapshot> locationSnapshot = dataSnapshot.getChildren().iterator();
  84. do {
  85. HashMap<String, String> locationMap = new HashMap<String, String>();
  86. Location location = new Location();
  87. String locId = locationSnapshot.next().getKey();
  88. Log.d(TAG, "locId: " + locId);
  89. location = dataSnapshot.child(locId).getValue(Location.class);
  90. Log.d(TAG, "Location name: " + location.getLocName());
  91. locationData.add(location);
  92. } while (locationSnapshot.hasNext());
  93. progressBar.setVisibility(View.GONE);
  94. setUI(locationData);
  95. }
  96.  
  97. @Override
  98. public void onCancelled(DatabaseError databaseError) {
  99.  
  100. }
  101. });
  102. }
  103.  
  104. private void setUI(List<Location> locations) {
  105. GridAdapter adapter = new GridAdapter(getApplicationContext(), locations);
  106. gvLocation.setAdapter(adapter);
  107. }
  108.  
  109. @Override
  110. protected void onStart() {
  111. super.onStart();
  112. mAuth.addAuthStateListener(mAuthListener);
  113. }
  114.  
  115. @Override
  116. protected void onStop() {
  117. super.onStop();
  118. if (mAuthListener != null) {
  119. mAuth.removeAuthStateListener(mAuthListener);
  120. }
  121. }
  122.  
  123. @Override
  124. public boolean onCreateOptionsMenu(Menu menu) {
  125. getMenuInflater().inflate(R.menu.menu_main, menu);
  126. return true;
  127. }
  128.  
  129. @Override
  130. public boolean onOptionsItemSelected(MenuItem item) {
  131. int id = item.getItemId();
  132. if (id == R.id.action_sign_out) {
  133. mAuth.signOut();
  134. return true;
  135. }
  136. return super.onOptionsItemSelected(item);
  137. }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement