copyright© jeffrey jongko, ateneo de manila university custom listadapters
TRANSCRIPT
![Page 1: Copyright© Jeffrey Jongko, Ateneo de Manila University Custom ListAdapters](https://reader035.vdocuments.us/reader035/viewer/2022062713/56649f3e5503460f94c5f4d0/html5/thumbnails/1.jpg)
Copyright© Jeffrey Jongko, Ateneo de Manila University
CustomListAdapters
![Page 2: Copyright© Jeffrey Jongko, Ateneo de Manila University Custom ListAdapters](https://reader035.vdocuments.us/reader035/viewer/2022062713/56649f3e5503460f94c5f4d0/html5/thumbnails/2.jpg)
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
![Page 3: Copyright© Jeffrey Jongko, Ateneo de Manila University Custom ListAdapters](https://reader035.vdocuments.us/reader035/viewer/2022062713/56649f3e5503460f94c5f4d0/html5/thumbnails/3.jpg)
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
![Page 4: Copyright© Jeffrey Jongko, Ateneo de Manila University Custom ListAdapters](https://reader035.vdocuments.us/reader035/viewer/2022062713/56649f3e5503460f94c5f4d0/html5/thumbnails/4.jpg)
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
![Page 5: Copyright© Jeffrey Jongko, Ateneo de Manila University Custom ListAdapters](https://reader035.vdocuments.us/reader035/viewer/2022062713/56649f3e5503460f94c5f4d0/html5/thumbnails/5.jpg)
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
![Page 6: Copyright© Jeffrey Jongko, Ateneo de Manila University Custom ListAdapters](https://reader035.vdocuments.us/reader035/viewer/2022062713/56649f3e5503460f94c5f4d0/html5/thumbnails/6.jpg)
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
![Page 7: Copyright© Jeffrey Jongko, Ateneo de Manila University Custom ListAdapters](https://reader035.vdocuments.us/reader035/viewer/2022062713/56649f3e5503460f94c5f4d0/html5/thumbnails/7.jpg)
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
![Page 8: Copyright© Jeffrey Jongko, Ateneo de Manila University Custom ListAdapters](https://reader035.vdocuments.us/reader035/viewer/2022062713/56649f3e5503460f94c5f4d0/html5/thumbnails/8.jpg)
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
![Page 9: Copyright© Jeffrey Jongko, Ateneo de Manila University Custom ListAdapters](https://reader035.vdocuments.us/reader035/viewer/2022062713/56649f3e5503460f94c5f4d0/html5/thumbnails/9.jpg)
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
![Page 10: Copyright© Jeffrey Jongko, Ateneo de Manila University Custom ListAdapters](https://reader035.vdocuments.us/reader035/viewer/2022062713/56649f3e5503460f94c5f4d0/html5/thumbnails/10.jpg)
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; }
![Page 11: Copyright© Jeffrey Jongko, Ateneo de Manila University Custom ListAdapters](https://reader035.vdocuments.us/reader035/viewer/2022062713/56649f3e5503460f94c5f4d0/html5/thumbnails/11.jpg)
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>