ListView View --- using ListView instance explicitly to customize ListActivity interface to have more than just the ListView in the GUI or to cutomize the look of the ListView in 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 has a default layout that consists of a single, full-screen list in the center of the screen.
can customize the screen layout by setting your own view layout with setContentView() in onCreate(). To do this, your own view MUST contain a ListView object with the id "@android:id/list" (or android.R.id.list if it's in code)
- Create ListActivity class in its XML gui interface (see below full main.xml file)
<ListView android:id="@+id/android:list" android:layout_width="wrap_content" android:layout_height="wrap_content" >>> Note we are specify a button below to come afer the ListView ---This controls so that the ListView
automatically included in the ListActivity will come before the button>>>>see main.xml below
- Setup of Get the Strings that represent the Items in the List in our previous example with the built-in ListView in the ListActivity we hard coded them. We could look them up from file or database. Another alternative is to store them in thre res/values/stirngs.xml file
- grab a handle to our ListView View defined in XML in the code using the getListView() inherited from the ListActivity class. (remember the ListView defined in the xml is the same as the List
ListView lstView = getListView();
- in onCreate(*) of our Activity setup the ListAdapter using setListAdapter
setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_checked, 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
- Implement inherited method in ListActivity that is called automatically for event handling on the ListView -- onListItemClick
onListItemClick(ListView l, View v, int position, long id) This method will be called when an item in the list is selected. - EXAMPLE
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, BasicViews5Activity!</string>
<string name="app_name">BasicViews5</string>
<string-array name="presidents_array">
<item>Dwight D. Eisenhower</item>
<item>John F. Kennedy</item>
<item>Lyndon B. Johnson</item>
<item>Richard Nixon</item>
<item>Gerald Ford</item>
<item>Jimmy Carter</item>
<item>Ronald Reagan</item>
<item>George H. W. Bush</item>
<item>Bill Clinton</item>
<item>George W. Bush</item>
<item>Barack Obama</item>
</string-array>
</resources> **************************** In ListActivity.java class you must grab array associated with this <string-array> from xml presidents = getResources().getStringArray(R.array.presidents_array);
main.xml ( activities defined GUI in xml) <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/btn" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Show selected items" android:onClick="onClick"/> <ListView
android:id="@+id/android:list"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout> |
res/values/strings.xml ---where we stored the presidents string array
|
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) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView lstView = getListView(); //lstView.setChoiceMode(ListView.CHOICE_MODE_NONE); //lstView.setChoiceMode(ListView.CHOICE_MODE_SINGLE); lstView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); //setting up so can select more than one element lstView.setTextFilterEnabled(true); presidents = getResources().getStringArray(R.array.presidents_array);
setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_checked, presidents)); } //event handler for ListView that is part of ListActivity --- method inherited from ListActivity } |