Adapters in Android

An Adapter object acts as a bridge between an AdapterView and the implicit in data for that opinion. The Adapter provides access to the datum items. The Adapter is besides responsible for making a scene for each detail in the datum stage set. Regarding the challenge, we have to display the list of criminals. This can be done using ListView and ListFragment. We need an adapter to bind the data fetched from the database to corresponding row in the list. On clicking rows of listview, we need to show the details of the equate condemnable. This can be done by adding setOnClickListner() to row detail in the list.

Reading: Adapters


fragment is a slice of activity ’ s UI. We prefer to create the drug user interface using fragments and add these fragments to activity therefore that they can be reused. It ’ sulfur lifecycle is like to that of the action. ListFragment displays a number of items by binding to a data reference such as an array or Cursor, and exposes event handlers when the exploiter selects an detail. It hosts a ListView object that can be bound to different data sources, typically either an array or a Cursor holding question results .

How do we add Fragment to Activity?

The be snip illustrates how doctor of osteopathy we add the break up to natural process. This should be done in the onCreate method of activity .

 fManager = getSupportFragmentManager ( ) ; 
 fTranslation = fManager.beginTransaction ( ) ; 
 exampleListFragment = raw ExampleListFragment ( ) ; 
 fTranslation.replace (, exampleListFragment ) ; 
 fTranslation.commit ( ) ;

The exampleListFragment is declared as :

 populace class ExampleListFragment extends ListFragment { ListView listview ; 
 List  rowlist;
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { View v = inflater.inflate ( R.layout.list_fragment, 
 container, fake ) ; 
 listview = ( ListView ) v.findViewById ( ) ; //data to be binded with list 
 rowlist = new ArrayList (); rowlist.add ( new RowItem ( ... .. ) ) ; 
 ... //adding items to rowlist //adding adapter to listview 
 list.setAdapter ( new RowListAdapter ( context, resultant role ) ) ; //returns ListFragment ViewGroup 
 restitution five ; 
 } }

ListView is a ViewGroup which shows number of scrollable items. ( ViewGroup is a special see that can contain other views. The ViewGroup is the base class for layouts and views containers. ) here is quick tutorial on ListFragment using AndroidStudio.


Adapter in android acts as bridge between an AdapterView and the underlying data for that opinion. The Adapter provides access to the datum items. The Adapter is besides creditworthy for making a horizon for each detail in the data typeset . Data can be information obtained from database or server or it can be already present in plan. There are many subclasses of Adapter, for exercise CursorAdapter, ArrayAdapter etc .


BaseAdapter is an abstract class which implements ListAdapter and SpinnerAdapter Interface. Hence, we may use it for implementing both ListView and Spinner. Following methods are necessary to implement for creating a subclass of BaseAdapter :

  • getCount() : int returns number of rows in list
  • getItem(int position): Object returns Item object
  • getItemId(int position): long returns the id of the item at position position
  • getView(int arg0, View arg1, ViewGroup arg2): View

How getView() works?

Adapters call the getView() method acting which returns a watch for each token within the arranger view. The layout format and the corresponding data for an detail within the arranger view is set in the getView() method. due to performance reasons, getView() doesn ’ thymine returns a new View object every time it is called. So what Android does is that it recycles the views and reuses the view that goes out of focus .


Cursor is an interface which provides random read-write access to result set returned by read write question. This interface has been explained in our previous post on databases in android. CursorAdapter is a subclass of BaseAdapter. It is used to bind cursor data qureied from database to AdapterView. Cursor adapter has following abstract methods ,

  • newView(Context context, Cursor cursor, ViewGroup parent): View This method creates new AdapterView to hold data.
  • bindView(View view, Context context, Cursor cursor): void In this method CursorAdapter just elements to list’s view.

SimpleCursorAdapter is subclass of CursorAdapter. If we use it, we don ’ t have to create branch Adapter ’ sulfur subclass. here is a sample code that shows how do we attach this to listview .

 // The craved column to be bound 
 String [ ] columns = new String [ ] { People.NAME, People.NUMBER } ; // XML defined views which the data will be bound to 
 int [ ] to = newfangled int [ ] {, } ; 
 // Create the arranger using the cursor pointing to the desired data equally well as the layout information 
 SimpleCursorAdapter mAdapter = newfangled SimpleCursorAdapter ( this, R.layout.list_example_entry, cursor, column, to ) ; // Set this arranger as your list 's adapter 
 this.setListAdapter ( mAdapter ) ;

Array Adapter

Array Adapter is subclass of BaseAdapter. It is used to bind data in arrayList to AdapterView. In this type of arranger, we pass List, context of activity and layout of entry as attribute, but layout of entry can be only those which are already present in the library. We associate an adapter with View.

Custom Adapters for ListView

Custom Adapter is used when we want to design and add more UI components to an token of ListView or GridView. We build this class on our own. This class extends Adapter or Adapter ’ second subclass. In the getView ( ) method of course, we may set custom-made capacity View . trope : ListView using CustomAdapter In general, CustomArrayAdapter overrides the comply methods of ArrayAdapter, example code : getView ( )

 populace View getView ( int position, View convertView, ViewGroup parent ) 
 View entryView = convertView ; 
 if ( convertView == nothing ) { 
 LayoutInflater layoutInflater = ( LayoutInflater ) context.getSystemService ( Context.LAYOUT_INFLATER_SERVICE ) ; 
 entryView = layoutInflater.inflate ( R.layout.entry_item, parent, false ) ; 
 TextView entryUser = ( TextView ) entryView.findViewById ( ) ; 
 //and therefore on, add all UI components 
 //setOnClickListener events to button components render entryView ; 

similarly, we can create CustomCursorAdapter by extending CursorAdapter. SubClass of CursorAdapter must have to override the pursue methods of CursorAdapter. hera is sample code for newView() and bindView() abstract methods of CursorAdapter .

 *Makes a raw view to hold the data pointed to by cursor. 
 @ Override 
 public View newView ( Context context, Cursor cursor, ViewGroup parent ) 
 LayoutInflater inflater = LayoutInflater.from ( context ) ; 
 View newView = inflater.inflate ( 
 android.R.layout.simple_list_item_1, parent, assumed ) ; 
 return newView ; 
 } @ Override 
 populace void bindView ( View scene, Context context, Cursor cursor ) 
 TextView txtView = ( TextView ) view.findViewById ( ) ; 
 txtView.setText ( cursor.getString ( 
 cursor.getColumnIndex ( `` person '' ) ) ) ; 

here is a immediate tutorial on CustomCursorAdapter. This concludes the post on adapters. Hope you will now be able to implement lists in your app. Keep watch for our future post on google maps. Till then adieu and glad code !

beginning :
Category : Tech

About admin

I am the owner of the website, my purpose is to bring all the most useful information to users.

Check Also


Manage participants in a zoom meeting webinar

Call the people who attend the meet as follows Alternate host host Who scheduled the …

Leave a Reply

Your email address will not be published.