Advertisement
kitlolz012

hutel

Nov 17th, 2023
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 KB | None | 0 0
  1. package com.example.gypsy;
  2.  
  3. import androidx.appcompat.app.AppCompatActivity;
  4. import android.content.Intent;
  5. import android.net.Uri;
  6. import android.os.Bundle;
  7. import android.widget.LinearLayout;
  8. import android.widget.Toast;
  9. import com.google.android.material.button.MaterialButton;
  10. import com.google.firebase.database.DataSnapshot;
  11. import com.google.firebase.database.DatabaseError;
  12. import com.google.firebase.database.DatabaseReference;
  13. import com.google.firebase.database.FirebaseDatabase;
  14. import com.google.firebase.database.ValueEventListener;
  15.  
  16. public class MainActivity5 extends AppCompatActivity {
  17.  
  18. @Override
  19. protected void onCreate(Bundle savedInstanceState) {
  20. super.onCreate(savedInstanceState);
  21. setContentView(R.layout.activity_main5);
  22.  
  23. Intent intent = getIntent();
  24. if (intent != null) {
  25. String hotelId = intent.getStringExtra("hotelId");
  26.  
  27. if (hotelId != null) {
  28. LinearLayout linearLayout = findViewById(R.id.linearLayout);
  29. queryHotelDetails(hotelId, linearLayout);
  30. } else {
  31. // Handle the case where hotelId is null
  32. Toast.makeText(MainActivity5.this, "Hotel ID is null", Toast.LENGTH_SHORT).show();
  33. }
  34. }
  35. }
  36.  
  37. private void queryHotelDetails(String hotelId, LinearLayout linearLayout) {
  38. DatabaseReference hotelRef = FirebaseDatabase.getInstance().getReference().child("hotels").child(hotelId);
  39. hotelRef.addListenerForSingleValueEvent(new ValueEventListener() {
  40. @Override
  41. public void onDataChange(DataSnapshot dataSnapshot) {
  42. if (dataSnapshot.exists()) {
  43. String hotelName = dataSnapshot.child("name").getValue(String.class);
  44. double latitude = dataSnapshot.child("latitude").getValue(Double.class);
  45. double longitude = dataSnapshot.child("longitude").getValue(Double.class);
  46.  
  47. // Create a button with the hotel name
  48. createButtonForHotel(linearLayout, hotelId, hotelName, latitude, longitude);
  49. }
  50. }
  51.  
  52. @Override
  53. public void onCancelled(DatabaseError databaseError) {
  54. // Handle the database error
  55. Toast.makeText(MainActivity5.this, "Database error: " + databaseError.getMessage(), Toast.LENGTH_SHORT).show();
  56. }
  57. });
  58. }
  59.  
  60. private void createButtonForHotel(LinearLayout linearLayout, String hotelId, String hotelName, double latitude, double longitude) {
  61. // Create a new MaterialButton for the hotel
  62. MaterialButton directionBtn = new MaterialButton(MainActivity5.this);
  63. directionBtn.setText("Navigate to " + hotelName);
  64.  
  65. // Set click listener for the button
  66. directionBtn.setOnClickListener(v -> {
  67. directionFromCurrentMap(latitude, longitude);
  68. });
  69.  
  70. // Add the button to the LinearLayout
  71. linearLayout.addView(directionBtn);
  72. }
  73.  
  74. private void directionFromCurrentMap(double destinationLatitude, double destinationLongitude) {
  75. Uri mapUri = Uri.parse("https://maps.google.com/maps?daddr=" + destinationLatitude + "," + destinationLongitude);
  76. Intent intent = new Intent(Intent.ACTION_VIEW, mapUri);
  77. startActivity(intent);
  78. }
  79. }
  80.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement