Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Java code for Main Activity 5 :
- package com.example.gypsy;
- import androidx.appcompat.app.AlertDialog;
- import androidx.appcompat.app.AppCompatActivity;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.net.Uri;
- import android.os.Bundle;
- import android.widget.LinearLayout;
- import android.widget.Toast;
- import com.google.android.material.button.MaterialButton;
- 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 MainActivity5 extends AppCompatActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main5);
- Intent intent = getIntent();
- if (intent != null) {
- String hotelId = intent.getStringExtra("hotelId");
- if (hotelId != null) {
- LinearLayout linearLayout = findViewById(R.id.linearLayout);
- queryHotelDetails(hotelId, linearLayout);
- } else {
- // Handle the case where hotelId is null
- Toast.makeText(MainActivity5.this, "Hotel ID is null", Toast.LENGTH_SHORT).show();
- }
- }
- }
- private void queryHotelDetails(String hotelId, LinearLayout linearLayout) {
- DatabaseReference hotelRef = FirebaseDatabase.getInstance().getReference().child("hotels").child(hotelId);
- hotelRef.addListenerForSingleValueEvent(new ValueEventListener() {
- @Override
- public void onDataChange(DataSnapshot dataSnapshot) {
- if (dataSnapshot.exists()) {
- String hotelName = dataSnapshot.child("name").getValue(String.class);
- double latitude = dataSnapshot.child("latitude").getValue(Double.class);
- double longitude = dataSnapshot.child("longitude").getValue(Double.class);
- // Create a button with the hotel name
- createButtonForHotel(linearLayout, hotelName, latitude, longitude);
- }
- }
- @Override
- public void onCancelled(DatabaseError databaseError) {
- // Handle the database error
- Toast.makeText(MainActivity5.this, "Database error: " + databaseError.getMessage(), Toast.LENGTH_SHORT).show();
- }
- });
- }
- private void createButtonForHotel(LinearLayout linearLayout, String hotelName, double latitude, double longitude) {
- // Create a new MaterialButton for the hotel
- MaterialButton directionBtn = new MaterialButton(MainActivity5.this);
- directionBtn.setText("Navigate to " + hotelName);
- // Set click listener for the button
- directionBtn.setOnClickListener(v -> {
- // Show an alert dialog indicating that the map coordinates may not be accurate
- showAlertDialog(hotelName, latitude, longitude);
- });
- // Add the button to the LinearLayout
- linearLayout.addView(directionBtn);
- }
- private void showAlertDialog(String hotelName, double latitude, double longitude) {
- AlertDialog.Builder builder = new AlertDialog.Builder(this);
- builder.setTitle("Map Coordinates Not Accurate");
- builder.setMessage("The map coordinates for " + hotelName + " may not be accurate. Please consider manually searching for the location if its in correct .");
- builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // Proceed with navigating to the location on the map
- directionFromCurrentMap(latitude, longitude);
- }
- });
- builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // Cancel the navigation
- dialog.dismiss();
- }
- });
- // Show the alert dialog
- builder.show();
- }
- private void directionFromCurrentMap(double destinationLatitude, double destinationLongitude) {
- Uri mapUri = Uri.parse("https://maps.google.com/maps?daddr=" + destinationLatitude + "," + destinationLongitude);
- Intent intent = new Intent(Intent.ACTION_VIEW, mapUri);
- startActivity(intent);
- }
- }
- XML CODE :
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- android:id="@+id/linearLayout"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- android:gravity="center"
- android:padding="10dp"
- tools:context=".MainActivity5">
- </LinearLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement