Creating A Custom Adapter

To use an Adapter with a ListView you must first extends an Adapter.

Create a List member for the data container.

m_data will contain all string items that you will want to be displayed in the ListView. So in our constructor we will populate m_data with the strings we want to be shown.

Then you must implement the methods getCount(), getItem(), getItemId() and getView().

getCount() must return the number of items that are currently in the Adapter. To do so we can just return the size of the member m_data.

For getItem(), this must return an Object and accepts an integer parameter for the location of the item. This means that we can use this later in our code to get the data of the item we want but you can also return a null value here.

getItemId() must return the id for the item in a given location. For the Adapter we have we can also return the location as an Id if you have already an Id for a given item then you must return it here.

getView() this one is where we will return the View that we want to show to the user. We can use a layout then inflate it here. For the layout, we will create one with only a TextView where we can show the String we have for that position from our m_data.

then for our getView() we will inflate the layout then set the String and return it for rendering.

After creating your Adapter for the ListView you can use the setAdapter() to make the ListView use your custom Adapter.

Running this in your device should look something like this:

Optimizing The Adapter

Currently on our implementation of getView() everytime a new item will be shown in the ListView we will create/inflate a new layout. Depending on what you are displaying this can be a big overhead and to minimize this we can use the 2nd parameter. The 2nd parameter is the view returned to us by the system that we can recycle instead of creating a new instance but theres a catch since this might be set to null if there are no views that can be recycled.

As you can see we are checking if the convertView is null. If it is then we must create a new instance of the view. You can download the sample project files from here.

Next: Android: Using An Adapter For Dynamic ListView