ListView Views through ListActivity
--see (Books/Android/Source Code/BasicViews5)
ListView (android.widget.ListView) is used to display list of items in a vertically scrolling list.
- SpinnerView is a subclass ---here have a spinner instead of scrolling list.
ListActivity - this is an subclass of Activity that includes a ListView in it automatically
- Create Activity class that extends ListActivity
- Setup of Get the Strings that represent the Items in the List here we are going to store it in an array of strings called presidents[]
public class BasicViews5Activity extends ListActivity {
- in onCreate(*) of our ListActivity setup the ListAdapter using setListAdapter
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, presidents));
- Implement the onListItemClick() method that is an eventhandler code to respond when an Item in ListActivity's built-in ListView is selected
- See full example below
- See full example below
- EXAMPLE
String[] presidents = {"Dwight D Eisenhower", ********};
ListActivity Example |
BasicViews5Activity.java package net.learn2develop.BasicViews5; import android.app.ListActivity; import android.os.Bundle; import android.view.View; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast; public class BasicViews5Activity extends ListActivity { String[] presidents; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, presidents)); } public void onListItemClick( ListView parent, View v, int position, long id) { Toast.makeText(this, presidents[position], Toast.LENGTH_LONG).show(); } } |