Upload
carmella-bruce
View
226
Download
7
Tags:
Embed Size (px)
Citation preview
Copyright© Jeffrey Jongko, Ateneo de Manila University
CustomListAdapters
ListAdapter
Acts as the bridge between a ListView and the data that backs the list (the model)
Interface found in android.widget package
Several subtypes are built-in to android
Custom ListAdapters
The true power of the ListView/ ListAdapter combination is the ability to define your own ListAdapter to best suit you application
ListAdapter is simply an interface that needs to be implemented
ListAdapter interface ListAdapter contains several methods inside it that need to
be implemented
Some methods:
public int getCount() - returns the number of items
public Object getItem(int arg0) – returns the object at a given position
public long getItemId(int position) – return a unique ID for a given position
ListAdapter interface
public View getView(int position, View convertView, ViewGroup parent) – returns a view object used to render the data convertView represents an existing recyclable
view that can be modified instead of creating a new view
All the basic ListAdapter methods are implemented in a class BaseAdapter You will need to subtype this and implement the
above methods
Application data Many times application data comes in the form of
lists of objects rather than lists of simple Strings E.g. contacts are composed
Name Phone Number E-mail etc
This is the type of data that is best served by custom adapter classes since you will want to present all these somehow to the user
Creating your own Adapter
The most common customization to an adapter is the row’s layout
Given the many methods present in the ListAdapter interface, it is usually better to subclass BaseAdapter and fill the missing methods as stated earlier
getView()
public View getView(int position, View convertView, ViewGroup parent) – returns a view object used to render the data position – represents the position in your
dataset convertView - represents an existing recyclable
view that can be modified instead of creating a new view
parent – represents the parent (usually the ListView object) that holds this view
Custom views
Views used for layout rows can be easily created using the Graphical EditorCreated the same way as with an Activity
layoutThese are the parsed using a
LayoutInflater instance inside getView() and populated with the proper data
Example
NOTE: getLayoutInflater() is in the Activity class
i.e. you need access to the Activity to use it either as an inner class or pass the Activity to the Adapter
public View getView(int position, View convertView, ViewGroup parent) { LayoutInflater inflater = getLayoutInflater(); View view = inflater.inflate(R.layout.row, null); // extract the views to be populated TextView name = (TextView) view.findViewById(R.id.name); TextView phone = (TextView) view.findViewById(R.id.phone); TextView email = (TextView) view.findViewById(R.id.email); // extract the object that will fill these MyContact contact = internalList.get(position); name.setText(contact.getName()); phone.setText(contact.getPhone()); email.setText(contact.getEmail()); // return the view return view; }
Output vs XML
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:text="TextView" android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> <TextView android:text="TextView" android:id="@+id/phone" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> <TextView android:text="TextView" android:id="@+id/email" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView></LinearLayout>