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;
  this.resTitle =R.id.title;
  this.resNumber =R.id.number;
 }

 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;
 }

 @Override
 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,
                          parent,
                          false);
   viewHolder.title =
          (TextView) row.findViewById(resTitle);
   viewHolder.number =
          (TextView) row.findViewById(resNumber);
   row.setTag(viewHolder);
  } else {
   row = convertView;
   viewHolder =
         (ViewHolder) convertView.getTag();
  }

  viewHolder.title.setText((CharSequence)
                     objects[position].myTitle);
  viewHolder.number.setText(Integer.toString(
                     objects[position].myNum));
  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

Androidgears

 

 

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.

 

square

 



 

Comments




or email your comment to: comments@i-programmer.info

 

setContentView(R.layout.activity_main);


Last Updated ( Saturday, 15 October 2016 )