3.1 Adapters, BaseAdapter, ArrayAdapter & AdapterView
An Adapter acts as a bridge between an AdapterView (such as ListView, GridView, Spinner, RecyclerView) and the underlying data source (such as an ArrayList, Array, or SQLite Cursor). It converts individual data items into rendered row views.
Types of Adapters:
- ArrayAdapter: Built-in adapter that binds an Array or
List<T>of objects to a singleTextViewper row (e.g.,android.R.layout.simple_list_item_1). - BaseAdapter: Abstract root adapter providing complete customization for complex multi-widget rows. Requires overriding 4 core methods:
int getCount(): Returns total number of data items in collection.Object getItem(int position): Returns data item at specified index.long getItemId(int position): Returns unique row ID for the item.View getView(int position, View convertView, ViewGroup parent): Inflates custom XML row layout and binds data to view widgets.
- ViewHolder Pattern: Reuses inflated row views (
convertView) and caches subview references inside a static class, eliminating repetitivefindViewById()calls and providing smooth 60 FPS scrolling.