Android Adventures - ListView And Adapters
Written by Mike James   
Thursday, 08 October 2015
Article Index
Android Adventures - ListView And Adapters
Interacting with lists
Custom ArrayAdapter
Improving Efficiency, Summary
Custom Adapter - Complete Listing


Custom Adapter

public class MyAdapter extends ArrayAdapter<MyData> {
 private Context context;
 private int resource;
 private MyData[] objects;
 private int resTitle;
 private int resNumber;

 public MyAdapter(Context context,
                  int resource,
                  MyData[] objects) {
  super(context, resource, objects);
  this.context = context;
  this.resource = resource;
  this.objects = objects;

 public MyAdapter(Context context,
                  int resource,
                  int resTitle,
                  int resNumber,
                  MyData[] objects) {
  super(context, resource, objects);
  this.context = context;
  this.resource = resource;
  this.resTitle = resTitle;
  this.resNumber = resNumber;
  this.objects = objects;

 private static class ViewHolder {
  TextView title;
  TextView number;

 public View getView(int position,
                     View convertView,
                     ViewGroup parent) {
  View row;
  ViewHolder viewHolder;
  if (convertView == null) {
   viewHolder = new ViewHolder();
   LayoutInflater inflater =
       ((Activity) context).getLayoutInflater();
   row = inflater.inflate(resource,
   viewHolder.title =
          (TextView) row.findViewById(resTitle);
   viewHolder.number =
          (TextView) row.findViewById(resNumber);
  } else {
   row = convertView;
   viewHolder =
         (ViewHolder) convertView.getTag();

  return row;



You can download the code for the programs from the CodeBin (note you have to register first).


androidJavaSmallAndroid Programming In Java:
Starting With an App
Third Edition

Is now available in paperback and ebook.

Available from Amazon.



  1. Getting Started With Android Studio 3
  2. The Activity And The UI
  3. Building The UI and a Calculator App
  4. Android Events
         Extract: Using Lambdas
  5. Basic Controls
  6. Layout Containers
  7. The ConstraintLayout
        Extract: Guidelines and Barriers
  8. UI Graphics A Deep Dive
        Extract: Programming the UI ***NEW
  9. Menus & The Action Bar
  10. Menus, Context & Popup
  11. Resources
  12. Beginning Bitmap Graphics
        Extract: Simple Animation
  13. Staying Alive! Lifecycle & State
  14. Spinners
  15. Pickers
  16. ListView And Adapters

If you are interested in creating custom template also see:

Custom Projects In Android Studio




To be informed about new articles on I Programmer, install the I Programmer Toolbar, subscribe to the RSS feed, follow us on, Twitter,FacebookGoogle+ or Linkedin,  or sign up for our weekly newsletter.


raspberry pi books



or email your comment to:



Last Updated ( Saturday, 15 October 2016 )