0% found this document useful (0 votes)
55 views

Lecture08 Adapters

An adapter object acts as a bridge between an AdapterView and the underlying data. It provides access to the data items and is responsible for creating Views for each item. Common AdapterViews include ListView, GridView, and Spinner. An ArrayAdapter is used to display arrays or lists of data in an AdapterView. To use an adapter, you define the AdapterView and data source in XML, initialize them in code, create an Adapter, attach it to the AdapterView, and implement item selection listeners.

Uploaded by

TONY BLANKS
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views

Lecture08 Adapters

An adapter object acts as a bridge between an AdapterView and the underlying data. It provides access to the data items and is responsible for creating Views for each item. Common AdapterViews include ListView, GridView, and Spinner. An ArrayAdapter is used to display arrays or lists of data in an AdapterView. To use an adapter, you define the AdapterView and data source in XML, initialize them in code, create an Adapter, attach it to the AdapterView, and implement item selection listeners.

Uploaded by

TONY BLANKS
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 17

Mobile programming.

User interface Adapters


What is an adapter object

 An Adapter object acts as a


bridge between an
AdapterView and the
underlying data for that view.
 It provides access to the data
items.
 It is also responsible for
making a View for each item
in the data set.
Types of Data Adapters

 Types of data adapter include:


 ArrayAdapter

 ListAdapter

 CursorAdapter
What is an AdapterView

• An AdapterView is a viewGroup that access data through Adapter


object
 Commonly used AdapterViews are:

 ListView, 

 GridView,

  Spinner 

 Photo Gallery 
AdapterView Responsibilities

 Two main responsibilities of AdapterView


1) Filling the layout with data received through the help of an
Adapter object
2) Handling user selections - when a user selects an item, the
adapterView performs some action
ArrayAdapter

 ArrayAdapter is used to wrap a Java array or java.util.List instance,

It takes the following parameters


1. Context: an instance of the current activity
2. Layout of organizing the items: e.g.
android.R.layout.simple_list_item_1 (as shown above)
3. The actual data source: such as array or list of items to show
Using adapter object
 Step one: Define a view object on an xml file
Using adapter object

 Step Two: Declare Reference variable within an activity class


that is in java file
 Example
Using adapter object

Step Three: initialize reference variables with ID of adapter view


This is done by calling findViewById() method within oncreate ()
method
Example
Using adapter object
 Step Four: Define the source of data with in oncreate method

 For example,
 An arraylist can be declared and initialized with data items as
follows:
Using adapter object

 Step Five: Define an Adapter object within onCreate() method

For example,
The following code creates an adapater object called dataAdapter_ob
and wraps the arraylist called campuses
Using adapter object

 Step Six: Define the layout style for organizing items on AdapterView

Example
Using adapter object
 Step seven: Register(attach) the data adapter object on the
adapter view

 Example
Using adapter object
 Step seven: Implement OnItemSelected listener for detecting
items selected by the user.

 Example
Using adapter object

 Step eight: Define the event handlers for that will be triggered
by the onItem selected Listener
Example:
The following code defines an event handler that will identify
and show the selected item
Using adapter object

 Step eight: Define the event handler for that will be triggered if
nothing is selected

You might also like