Native Adapters in Android

In most cases you won’t have to create your own Adapters from scratch. Android supplies a set of Adapters that can pump data from common data sources (including arrays and Cursors) into the native controls that extend Adapter View

Because Adapters are responsible both for supplying the data and for creating the Views that represent each item, Adapters can radically modify the appearance and functionality of the controls they’re bound to.

The following list highlights two of the most useful and versatile native Adapters:

ArrayAdapter — The Array Adapter uses generics to bind an Adapter View to an array of objects of the specified class. By default, the Array Adapter uses the toString value of each object in the array to create and populate Text Views. Alternative constructors enable you to use more complex layouts, or you can extend the class (as shown in the next section) to bind data to more complicated layouts

SimpleCursorAdapter — The Simple Cursor Adapter enables you to bind the Views within a layout to specific columns contained within a Cursor (typically returned from a Content Provider query). You specify an XML layout to inflate and populate to display each child, and then bind each column in the Cursor to a particular View within that layout. The adapter will create a new View for each Cursor entry and inflate the layout into it, populating each View within the layout using the Cursor’s corresponding column value.

Customizing the Array Adapter

By default, the Array Adapter uses the toString values of each item within an object array to populate a Text View within the layout you specify.

Customizing the Array Adapter

public class MyArrayAdapter extends ArrayAdapter<MyClass> {
 int resource;
 public MyArrayAdapter(Context context,
 int _resource,
 List<MyClass> items) {
 super(context, _resource, items);
 resource = _resource;
 }
 @Override
 public View getView(int position, View convertView, ViewGroup parent) {
 // Create and inflate the View to display
 LinearLayout newView;
 if (convertView == null) {
 // Inflate a new view if this is not an update.
 newView = new LinearLayout(getContext());
 String inflater = Context.LAYOUT_INFLATER_SERVICE;
 LayoutInflater li;
 li = (LayoutInflater)getContext().getSystemService(inflater);
 li.inflate(resource, newView, true);
 } else {
 // Otherwise we’ll update the existing View
 newView = (LinearLayout)convertView;
 }
 MyClass classInstance = getItem(position);
 // TODO Retrieve values to display from the
 // classInstance variable.
 // TODO Get references to the Views to populate from the layout.
 // TODO Populate the Views with object property values.
 return newView;
 }
}

Creating and applying an Adapter

ArrayList<String> myStringArray = new ArrayList<String>();
int layoutID = android.R.layout.simple_list_item_1;
ArrayAdapter<String> myAdapterInstance;
myAdapterInstance = 
 new ArrayAdapter<String>(this, layoutID, myStringArray);
myListView.setAdapter(myAdapterInstance);

Simple Cursor Adapter

The SimpleCursorAdapter is used to bind a Cursor to an Adapter View using a layout to define the UI of each row/item. The content of each row’s View is populated using the column values of the corresponding row in the underlying Cursor.

Creating a Simple Cursor Adapter

LoaderManager.LoaderCallbacks<Cursor> loaded = 
 new LoaderManager.LoaderCallbacks<Cu rsor>() {
 public Loader<Cursor> onCreateLoader(int id, Bundle args) {
 CursorLoader loader = new CursorLoader(MyActivity.this,
 CallLog.CONTENT_URI, null, null, null, null); 
 return loader;
 }
 public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
 String[] fromColumns = new String[] {CallLog.Calls.CACHED_NAME, 
 CallLog.Calls.NUMBER};
 int[] toLayoutIDs = new int[] { R.id.nameTextView, R.id.numberTextView};
 SimpleCursorAdapter myAdapter;
 myAdapter = new SimpleCursorAdapter(MyActivity.this,
 R.layout.mysimplecursorlayout,
 cursor,
 fromColumns,
 toLayoutIDs);
 myListView.setAdapter(myAdapter);
 }
 
 public void onLoaderReset(Loader<Cursor> loader) {} 
};
getLoaderManager().initLoader(0, null, loaded);

Leave a Comment