Presentation is loading. Please wait.

Presentation is loading. Please wait.

Android Topics Custom ArrayAdapters

Similar presentations


Presentation on theme: "Android Topics Custom ArrayAdapters"— Presentation transcript:

1 Android Topics Custom ArrayAdapters
What do we know so far? Research ListView and ArrayAdapter. What is a ListAdapter? How can we use it? Review Concrete Class, Abstract Class, and Interface In-class App: Contact App using our own custom ArrayAdapter Lab 5 Part 3 Exercise 2.

2 What do we know so far? 1. ListView + ArrayAdapter
2. Android provides a layout for inflating very basic individual items in alist: android.R.layout.simple_list_item_1

3 What do we know so far? R.layout.simple_list_item_1 Creating Data:
ArrayList<String> words = new ArrayList<String>(); Constructing a simple ArrayAdapter: ArrayAdapter<String> itemsAdapter; itemsAdapter = new ArrayAdapter<String> (this, android.R.layout.simple_list_item_1, words);

4 Problem? R.layout.simple_list_item_1 is too simple for most apps Most Apps require the inflation of more complex list items. Solution? Create our own ArrayAdapter class to use with our own list_item layout.

5 How do we construct a custom ArrayAdapter?
Begin by researching ListView and ArrayAdapter.

6 Explore Documentation
Examine the documentation of ListView. Question: What did you find?

7 Explore Documentation
Examine the documentation of ListView. Findings: ListView is an AdapterView. In order to display items in the ListView, we need to call setAdapter(ListAdapter). setAdapter(ListAdapter) associates an adapter with the ListView.

8 Explore ListView Documentation
Question: What type of object is required for setAdapter()?

9 ListView and setAdapter Documentation
Findings: setAdapter() requires an object of type ListAdapter. Our previous code used an ArrayAdapter, not a ListAdapter.

10 Explore ArrayAdapter Documentation
Question: What is the base class of ArrayAdapter?

11 Explore ArrayAdapter Documentation
Question: What is the base class of ArrayAdapter? Answer: BaseAdapter

12 Explore ListAdapter Documentation
Question: What is the base class of ListAdapter?

13 Explore ListAdapter Documentation
Question: What is the base class of ListAdapter? Answer: ListAdapter is an Interface. ListAdapter implements Adapter.

14 Explore ListAdapter Documentation
Question: What are the two abstract public methods for ListAdapter?

15 ListAdapter public abstract methods

16 How do all these pieces fit together?
We see that ArrayAdapter extends from BaseAdapter. BaseAdapter is an abstract class. BaseAdapter implements the ListAdapter interface. Since BaseAdapter implements a ListAdapter, BaseAdapter can be used whenever ListAdapter is required.

17 Quick Review!

18

19 What does this mean? ListView setAdapter() requires a ListAdapter as its input. List Adapter is an interface with no states implemented. BaseAdapter class is an abstract class. Implementation exists for some ListAdapter methods. The ArrayAdapter is a concrete class. All methods are implemented. We can create an object instance of a custom ArrayAdapter and use it in our app. We can pass our custom ArrayAdapter variable as the input to the ListView.

20 Adapter is a common pattern in Android
We will create an object instance of a custom ArrayAdapter and use it in our app. We can pass our custom ArrayAdapter variable as the input to the ListView.

21 Model View Controller The Adapter references the data source and knows how to present each item as a View. The ListView handles the display of Views on screen. The ListView also detect the user's touch gestures and maintains the position of the user within the entire list. The ArrayAdapter deals with the Views The ListView is in charge of the details of the user interface.

22 Practice Lab 5 III Exercise 2: Create a Custom ArrayAdapter to produce a scrollable Contacts App. Do NOT use a ScrollView.

23 public class ContactsAdapter extends ArrayAdapter<Contact> { context: The current context is used to inflate the layout file contactList: This ArrayList is the data we want to populate into the ListView object. The ArrayAdapter will expect list of Contact objects to display in a list public ContactsAdapter(Activity context, ArrayList<Contact> contactList) { /** * Initialize the ArrayAdapter's internal storage for * the context and the list. The second argument is used only when the * ArrayAdapter is populating a single TextView */ super(context, 0, contactList); } }

24 getView() : Provides a view for an AdapterView (ListView)
getView() : Provides a view for an AdapterView (ListView) position: The AdapterView position that is requesting a view convertView: The recycled view to populate. parent: The parent ViewGroup that is used for inflation Return the View for the position in the AdapterView @Override public View getView(int position, View convertView, ViewGroup parent) { //TASK 1: GET THE CONTACT OBJECT LOCATED AT THIS POSITION // IN THE LIST //TASK 2: CHECK IF THE EXISTING VIEW IS BEING REUSED // OTHERWISE INFLATE THE VIEW //TASK 3: REFERENCE EACH VIEW IN THE CONTACT ITEM AND // ADD THE DATA CONTENT //TASK 4: RETURN ITEM LAYOUT CONTAINING THE TEXTVIEWS AND PHOTO } }


Download ppt "Android Topics Custom ArrayAdapters"

Similar presentations


Ads by Google