These widgets are used for helping you with writing or for selecting elements.
These widgets rely on adaptors that offer you a common interface for the underlying data models of a data selection widget. They provide the data list and convert it into different specific views that are then used by the selection widget for display operations.
3.4.1 The selectors
There are 6 different types of selection lists:
- The basic list : ListView
- The combo box : Spinner
- GridView : displays choices on a grid and are often used for selecting images
- AutoCompleteTextView : uses the User input to filter data and display a data subset. Data is displayed as with Spinner.
- Galleries : they display data as a horizontal list and are often used for selecting images
3.4.2 An example for ListView with an activity that extends ListView :
This example shows a list and a text field. When an item of the list is clicked upon, the text field retrieves the value of the selected item.
There are 3 important things to take into account for this first example:
- The use of android.R.layout.simple_list_item_1 in the definition of the ArrayAdapter will define how the items of the list will be presented.
- The use of the tag @android :id/list as the identifier for the ListView means it will be automatically interpreted as being the principal list.
- My activity extends ListActivity.
/**
* @author mathias seguy
* @goals This class aims to show a simple List Activity
*/
public class ListViewTuto extends ListActivity {
/*** A text view */
TextView textView;
/*** The dummy data list */
String[] items = {"item1","item2","item3","item4","item5","item6" };
/** *The arrayAdapter that manage the data displayed */
ArrayAdapter<String> arrayAdapter;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
// Define the Adapter (Context, ListView Ressource, The items to display)
arrayAdapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
//or using string values stored in the resources:
String[] itemsFromRes=getResources().getStringArray(R.array.itemsList);
arrayAdapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
itemsFromRes);
//Set the ArrayAdapter to the ListActivity
setListAdapter(arrayAdapter);
//The text view that displays the current selected item
textView = (TextView) findViewById(R.id.text);
}
/* (non-Javadoc)@see android.app.ListActivity#onListItemClick(android.widget.ListView, android.view.View, int, long)
*/
public void onListItemClick(ListView parent, View v, int position, long id) {
textView.setText(items[position]);
}
}
Where the xml file that declares the graphic elements is:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false"
/>
LinearLayout>
This goes in strings.xml :
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, ListViewTuto!</string>
<string name="app_name">ListViewTuto2</string>
<string-array>
<item>items1</item>
<item>items2</item>
<item>items3</item>
<item>items4</item>
<item>items5</item>
<item>items6</item>
</string-array>
</resources>










