Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright© Jeffrey Jongko, Ateneo de Manila University Custom ListAdapters.

Similar presentations


Presentation on theme: "Copyright© Jeffrey Jongko, Ateneo de Manila University Custom ListAdapters."— Presentation transcript:

1 Copyright© Jeffrey Jongko, Ateneo de Manila University Custom ListAdapters

2 ListAdapter  Acts as the bridge between a ListView and the data that backs the list (the model)ListView  Interface found in android.widget package  Several subtypes are built-in to android

3 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

4 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

5 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

6 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

7 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

8 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

9 Custom views  Views used for layout rows can be easily created using the Graphical Editor  Created the same way as with an Activity layout  These are the parsed using a LayoutInflater instance inside getView() and populated with the proper data

10 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; }

11 Output vs XML <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent">


Download ppt "Copyright© Jeffrey Jongko, Ateneo de Manila University Custom ListAdapters."

Similar presentations


Ads by Google