public class MyListActivity extends ListActivity { @Override protected void onStart() { super.onStart(); // Perform initial setup of the activity ArrayAdapteradapter = new ArrayAdapter (this, android.R.layout.simple_list_item_1, new String[]{"Item 1", "Item 2", "Item 3"}); setListAdapter(adapter); } }
import android.widget.ListView; import android.widget.Toast; public class MyListActivity extends ListActivity { @Override protected void onStart() { super.onStart(); // Perform initial setup of the activity ListView listView = getListView(); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView parent, View view, int position, long id) { String item = (String) parent.getItemAtPosition(position); // Display a Toast message with the selected item Toast.makeText(getApplicationContext(), "Selected: " + item, Toast.LENGTH_SHORT).show(); } }); } }In this example, when the activity is started, the ListView is retrieved using the getListView() method and an OnItemClickListener is set on the ListView. When an item in the list is clicked, the selected item is retrieved using the getItemAtPosition() method and a Toast message is displayed showing the selected item. The android.app.ListActivity class is part of the Android framework and is included in the android.app package.