ListViews
ListViews in Andriod
Students will learn that how to set An ordered collection of selectable choices.
List adapters
- Adapter: Helps turn list data into listviewitems.
–common adapters: ArrayAdapter, CursorAdapter
- Syntax for creating an adapter:
ArrayAdapter<String> name = new ArrayAdapter<String>(activity, layout, array); the activityis usually this
- the default layoutfor lists is R.layout.simple_list_item_1
- get the arrayby reading your file or data source of choice
(it can be an array like String[], or a list like ArrayList<String>)
–Once you have an adapter, you can attach it to your list by calling the setAdaptermethod of the ListViewobject in the Java code.
Handling list events
- Unfortunately lists don’t use a simple onClickevent.
–The event listeners must be attached in the Java code.
List events
- List views respond to the following events:
–setOnItemClickListener(AdapterView.OnItemClickListener)
Listener for when an item in the list has been clicked.
–setOnItemLongClickListener(AdapterView.OnItemLongClickListener)
Listener for when an item in the list has been clicked and held.
–setOnItemSelectedListener(AdapterView.OnItemSelectedListener)
Listener for when an item in the list has been selected.
Introduction
The classic listbox widget in Android is known as ListView. Include one of these in your layout, invoke setAdapter() to supply your data and child views, and attach a listener via setOnItemSelectedListener() to find out when the selection has changed. With that, you have a fully-functioning listbox.
However, if your activity is dominated by a single list, you might well consider creating your activity as a subclass of ListActivity, rather than the regular Activity base class. If your main view is just the list, you do not even need to supply a layout—ListActivity will construct a full-screen list for you. If you do want to customize the layout, you can, so long as you identify your ListView as @android:id/list, so ListActivity knows which widget is the main list for the activity.
Lab Activities:
Static lists
static list: Content is fixed and known before the app runs.
–Declare the list elements in the strings.xml resource file.
<!–res/values/strings.xml –>
<resources>
<string-array name=”oses”>
<item>Android</item>
<item>iPhone</item>
…
<item>Max OS X</item>
</string-array>
</resources>
<!–res/layout/activity_main.xml –>
<ListView… android:id=”@+id/mylist” android:entries=”@array/oses” />
Dynamic lists
Content is read or generated as the program runs.
–Comes from a array, or a data file, or from the internet, etc.
–Must be set in the Java code.
–Suppose we have the following file and want to make a list from it:
// res/raw/oses.txt Android
iPhone
…
Max OS X
import java.util.*;
public class ArrayListExamples{
public static void main(String args[]) {
// Creating an empty array list
ArrayList<String> list = new ArrayList<String>();
// Adding items to arrayList
list.add(“Item1”);
list.add(“Item2”);
list.add(2, “Item3”); // it will add Item3 to the third position of array list
list.add(“Item4”);
// Getting the size of the list
intsize = list.size();
System.out.println(“The size of the list is: ” + size);
// Getting the element in a specific position
String item = list.get(0);
System.out.println(“The item is the index 0 is: ” + item);
// Retrieve elements from the arraylist
// loop using index and size list
System.out.println(“Retrieving items with loop using index and size list”);
for (inti= 0; i< list.size(); i++) {
System.out.println(“Index: ” + i+ ” -Item: ” + list.get(i));
} }
LIST ADAPTER ACTIVITY
ArrayList<String> myArray= …; // load or store data …….
ArrayAdapter<String> myAdapter= new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, myArray);
ListViewlist = (ListView) findViewById(R.id.mylist);
list.setAdapter(myAdapter);
LIST EVENT LISTENER ACTIVITY
OnCreate(…) {
list = (ListView) findViewById(R.id.listView);
list.setAdapter(myAdapter);
list.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, intposition, long id) {
tv1.setText(“AAAA == = “+ position);
}
LIST EVENT LISTENER ACTIVITY
ListViewlist = (ListView) findViewById(R.id.id);
list.setOnItemClickListener(
new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> list,
View row,
intindex, long rowID) {
// code to run when user clicks that item
…
}
}
);
Home Activities:
Here are some things you can try beyond those step-by-step instructions:
- See what the activity looks like if you use a Spinner instead of a ListView.
- Make the address field, presently an EditText widget, into an AutoCompleteTextView, using the other addresses as values to possibly reuse (e.g., for multiple restaurants in one place, such as a food ourt or mall).