activity_tourist_guide.xml
for the activity:
<!-- activity_tourist_guide.xml --> <LinearLayout 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:orientation="vertical" android:padding="16dp" tools:context=".TouristGuideActivity"> <ListView android:id="@+id/attractions_list_view" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast; public class TouristGuideActivity extends AppCompatActivity { private ListView attractionsListView; private String[] attractions; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_tourist_guide); // Initialize views attractionsListView = findViewById(R.id.attractions_list_view); // Get attractions from resources attractions = getResources().getStringArray(R.array.attractions); // Set up the list view ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, attractions); attractionsListView.setAdapter(adapter); // Set item click listener attractionsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { String selectedAttraction = attractions[position]; showAttractionDetails(selectedAttraction); } }); } private void showAttractionDetails(String attraction) { // Perform an action based on the selected attraction // For demonstration purposes, a toast message is shown Toast.makeText(this, attraction, Toast.LENGTH_SHORT).show(); } }
In this example, the TouristGuideActivity
class extends AppCompatActivity
.
Inside the onCreate
method, the layout view is initialized, and the attractions are retrieved from the resources as an array of strings.
The list view is set up using an ArrayAdapter
to display the attractions. Each item in the list is clickable, and when an item is clicked, the showAttractionDetails
method is called.
The showAttractionDetails
method performs an action based on the selected attraction. In this example, a toast message is shown with the selected attraction name.
Make sure to add the attractions
string array resource to the res/values/strings.xml
file:
<!-- strings.xml --> <resources> ... <string-array name="attractions"> <item>Eiffel Tower</item> <item>Taj Mahal</item> <item>Great Wall of China</item> <item>Machu Picchu</item> <item>Pyramids of Giza</item> </string-array> ... </resources>Remember to replace the
attractions
array with the actual attractions you want to display in your tourist guide app.This example provides a basic structure for a tourist guide app. You can customize it by adding more features such as displaying attraction details, images, maps, or integrating with external APIs for additional information.