Today I'll write about Adapters. According the official site about Android Development "Adapter object acts as a bridge between an AdapterView and the underlying data for that view". This object help us to display the data within a bound parent View creating child Views that represent the underlying data.
In this post, I will describe like you can use the native adapters and like you can create your own adapter.
At first we will study like use a simple native adapter. Android has by default some native Adapters. Among them, the ArrayAdapter is the most used. ArrayAdapter binds a array with the View and creates a child object for each array object. By default, the Array Adapter uses the toString value of each object to populate Text Views.
The code displayed below shows the ArrayAdapter:
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
//get reference from listView...
simpleListView =(ListView)findViewById(R.id.simpleListView);
//create a simple array to bound to listView...
Contact contacts[] = Util.getContactList();
//bound array to list using a simple list item...
ArrayAdapter<Contact> adapter = new ArrayAdapter<Contact>(this,android.R.layout.simple_list_item_1, contacts);
//set the adapter to list view...
simpleListView.setAdapter(adapter);
}
The static method Util.getContactList is a stub method which returns a Array object with some contacts.
public class Util {
public static Contact[] getContactList() {
Contact contactList[] = { new Contact("Josh", "555 333 666"),
new Contact("Paul", "333 555 666"),
new Contact("Jimmy", "555 333 444"),
new Contact("Will", "555 222 666"),
new Contact("Peter", "111 333 222") };
return contactList;
}
}
The Contact class has a overridden method toString() which returns the contact name.
@Override
public String toString() {
// TODO Auto-generated method stub
return this.getName();
}
When the application is started, each contact is shown on the ListView like the image below.
Now, we will see like create a own Adapter, this action will give us a lot of possibilities to create a Adapter which supply our needs with best practices.
For you create yours own array adapters, you need extend the ArrayAdapter class passing a type of object. See the code below:
public class ContactItemAdapter extends ArrayAdapter<Contact> {
int resource;
public ContactItemAdapter(Context context, int resource, Contact[] items) {
super(context, resource, items);
this.resource = resource;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout customizedItem;
Contact item = getItem(position);
if (convertView == null) {
customizedItem = new LinearLayout(getContext());
String inflater = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater li;
li = (LayoutInflater)getContext().getSystemService(inflater);
li.inflate(resource, customizedItem, true);
} else {
customizedItem = (LinearLayout) convertView;
}
TextView displayName =(TextView)customizedItem.findViewById(R.id.itemDisplayName);
TextView phoneNumber =(TextView)customizedItem.findViewById(R.id.itemPhoneNumber);
displayName.setText(item.getName());
phoneNumber.setText(item.getPhoneNumber());
return customizedItem;
}
}
We will study this code step by step. On the constructor is referenced the resource which used to display the data. The most important block of this code is the getView method. As you can see, at this point of the code is constructed the child View that shows the data. Within of method getView we can define like the data will be shown on the child view.
See that in this code we are referencing two TextView objects. These TextView objects are explicited in the XML file.
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" >
<TextView android:id="@+id/itemDisplayName" android:layout_width="match_parent" android:layout_height="wrap_content" />
<TextView android:id="@+id/itemPhoneNumber" android:layout_width="match_parent" android:layout_height="wrap_content" />
</LinearLayout>
Now see like this our adapter is being used:
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
//get reference from listView...
listViewContact =(ListView)findViewById(R.id.listViewContact);
//create a simple array to bound to listView...
Contact contacts[] = Util.getContactList();
//bound array to list using a simple list item...
ContactItemAdapter adapter = new ContactItemAdapter(this,R.layout.customized_item, contacts);
//set the adapter to list view...
listViewContact.setAdapter(adapter);
}
You can note that last example have some few differences about the first example. In this last example we are using a resource created by us to define like the data will be shown, while in the first example we are using a default resource from Android (android.R.layout.simple_list_item_1). Furthermore, now we can show more than one field for once what enable us create more interesting interfaces.
At soon we will return to this example, but we will bound the phone contact list to our application.
Thanks!