Skip to content

Custom BaseAdapters

January 13, 2010

Hey everyone,

So to end my series on using CursorAdapters, I present to you one way I learned how to use BaseAdapters, as well as an example the Android SDK provides which is pretty thorough and shows you another way to implement it.

public class ContactListCursorAdapter extends BaseAdapter {

/** Remember our context so we can use it when constructing views. */
private Context mContext;

* Hold onto a copy of the entire Contact List.
private List<ContactEntry> mItems = new ArrayList<ContactEntry>();

public ContactListCursorAdapter(Context context, ArrayList<ContactEntry> items) {
     mContext = context;
     mItems = items;

public int getCount() {
     return mItems .size();

public Object getItem(int position) {
     return mItems .get(position);

 /** Use the array index as a unique id. */
public long getItemId(int position) {
     return position;

* @param convertView
*            The old view to overwrite, if one is passed
* @returns a ContactEntryView that holds wraps around an ContactEntry
public View getView(int position, View convertView, ViewGroup parent) {
	ContactEntryView btv;
	if (convertView == null) {
	     btv = new ContactEntryView(mContext, mShow.get(position));
	} else {
	     btv = (ContactEntryView) convertView;
	     String name = mShow.get(position).getName();
	     String number = mShow.get(position).getNumber();
	     if (number != null) {
	          btv.setNumberText("Mobile: " + mShow.get(position).getNumber());
        return btv;

Where the ContactEntryView is defined as:

public class ContactEntryView extends LinearLayout {

private TextView mName, mNumber;

public ContactEntryView(Context context, ContactEntry contact) {
     mName = new TextView(context);
     mNumber = new TextView(context);

     String name = contact.getName();

     String number = contact.getNumber();
     mNumber.setText("Mobile: " + contact.getNumber());

     addView(mName, new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
     addView(mNumber, new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

public String getNameText() {
     return mName.getText().toString();

public String getNumberText() {
     return mNumber.getText().toString();

public void setNameText(String name) {

public void setNumberText(String number) {


And finally the ContactEntry class is just a simple object class:

public class ContactEntry {

private String mName;

private String mNumber;

public ContactEntry(String name, String number) {
     mName = name;
     mNumber = number;

public String getName() {
     return mName;

public String getNumber() {
     return mNumber;

public void setName(String name) {
     mName = name;

public void setNumber(String number) {
     mNumber = number;


Before I dive into what’s going on, take a second to try and see the relationship between this custom base adapter, the custom view that I created, as well as the new ContactEntry object that I created. Once the relationship becomes clear, you can start to see how the wrapper functions in these methods facilitate the creating and retrieving of views.

Now as you can see, the BaseAdapter is different from the SimpleCursorAdapter namely that it takes a List (or Array) instead of a Cursor (i.e. a wrapper around an SQL table). Once you pass in this list, the behavior is pretty similar in the sense that in the getView() method, you manually construct and set the contents of each sub-view in your View (in my case, I create a custom view with wrapper functions to help facilitate the setting of its contents). You can clearly see the analog of this in our Custom CursorAdapter Example where instead of grabbing the list object (i.e. ContactEntry in this case) and using the data stored in the object to populate our views, we use the position of the cursor and retrieve that row’s data and store those in our views.

Other than that though, the two cursors are almost the same, and one should expect this as the SimpleCursorAdapter is actually a sub-class of the BaseAdapter. However, performance wise there are big differences, and these I will address in the next post.

To see the other way of implementing a BaseAdapter, see:

Android SDK Example

The difference between mine and this one is that mine uses a JAVA defined view whereas the Android SDK example inflates an XML defined view.

For more on CursorAdapters in general, please visit

Happy coding.

– jwei

13 Comments leave one →
  1. warrior permalink
    January 19, 2010 4:06 am

    Hi Jwei,
    I tried executing the code which you have given but got an error
    ‘mshow cannot be resolved”
    can u help me how to figure it out..!!

  2. warrior permalink
    January 19, 2010 4:12 am

    Hi jwei,
    Do u have any idea bout how to Enhance Contact Provider to contain Presence Status..??

  3. January 21, 2010 1:27 am

    Hi warrior,

    Replace “mShow” with “mItems” and it’ll all work fine…

  4. January 21, 2010 1:31 am

    Thanks Gmork yea that was my bad it’s been edited in the code.

  5. Mark permalink
    March 16, 2011 9:15 am

    How about extending your post with how to do this when you aren’t writing your Row view in Java but with xml. How to use the Inflater in a BaseAdapter, when it seems that you can’t get to your R.layout.<>?

  6. nam permalink
    October 25, 2011 8:35 am


  7. Yoily permalink
    April 28, 2012 7:03 pm

    i still see the mShow in the code. Glad someone commented on it.


  1. Google App Engine – Bringing it Back To Android (4) « Think Android
  2. How to manage list views Checkboxes Automatically checking? | PHP Developer Resource
  3. Creating an Adapter to a CustomView : Android Community - For Application Development
  4. getView method of baseAdapter is not getting called : Android Community - For Application Development
  5. Android: Get the position of a ListView from the actual top : Android Community - For Application Development
  6. How to populate Contacts from Phone to Custom Listview? : Android Community - For Application Development

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: