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