Log in



Grokking the ListAdapter, Part 1 of N

Friday, June 15th, 2012 by

Here’s my first published tip regarding Android development…

i admit it – my first 4 or 5 days with Android i simply could not understand how to use the ListView and the related Adapter classes. i kept having to add 1 or 2 levels of containers between my data and the list view, and kept thinking, “certainly this is not intended to be so painful!” Finally, after hours of reading through example after example demonstrating SimpleAdapter and friends, i came across a comment post somewhere (now lost) which demonstrated a very simple anonymous subclass of ┬áBaseAdapter (far simpler than the jumping-through-hoops demonstrated by most examples i had seen). The following demonstrates a real live function (not a contrived example) which demonstrates how to use the ListAdapter:

// Callback used by various value-selection helpers in my app
public interface ValueSelectionCallback<T>{
    /**
     @param value The value selected.
     */
    void onSelect( T value );
}

// Now the function demonstrating BaseAdapter:
/**
 Prompts the user to select from a ListView of strings.

 @param context View context.
 @param title Optional title (may be 0 to indicate no title)
 @param names List of strings to display.
 @param callback Is passed the selected value.
 */
public static void stringSelectionPopup(final Context context,
                                        final int title,
                                        final List<String> names,
                                        final ValueSelectionCallback<String> callback){
    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    if( 0 != title ){
        builder.setTitle( title );
    }

    ListAdapter adapter = new BaseAdapter(){
        public int getCount() {
            return names.size();
        }

        public Object getItem(int i) {
            return names.get(i);
        }

        public long getItemId(int i) {
            return i;
        }

        public View getView(int i, View view, ViewGroup viewGroup) {
            TextView tv = (view instanceof TextView)
                 ? (TextView)view
                 : createTextViewForList(viewGroup.getContext())
                    /* INTERNAL HELPER, not demonstrated */;
            tv.setText( names.get(i) );
            return tv;
        }
    };
    builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialogInterface, int i) {
            callback.onSelect(names.get(i));
        }
    });
    builder.show();
}

Granted, that particular case can be implemented more simply using one of the built-in adapters, but the above demonstrates clearly how to use the BaseAdapter. Once that is understood, the ListView/ListAdapter model goes from being very painful to… well, still a little painful but very powerful (as far as list-view widgets go).

 

Happy Hacking!

—– stephan beal

Comments are closed.