SlideShare a Scribd company logo
1
Android UI Adapters
Topics
• AdapterView & Adapter
• AdapterView responsibilities
• ListActivity, ListView, and ListAdapter
• Spinner, SpinnerAdapter
AdapterView & Adapter
What is AdapterView?
• The AdapterView is a ViewGroup subclass
whose child Views are determined by an
Adapter that binds AdapterView object to data
of some type.
• Typically you are going to use subsclasses of
AdapterView class instead of using it directly
• Example subclasses of AdapterView class
> ListView
> Spinner
> Gallery
What is an Adapter?
• An Adapter object acts as a bridge between an
AdapterView and the underlying data for that
view.
> The Adapter provides access to the data items.
• The Adapter is also responsible for making a
View for each item in the data set.
• Types of Adatpers - they implements
ListAdatper interface
> ArrayAdatper
> CursorAdatper
> There are a few more
Adapter Class Hierarchy
• BaseAdatper abstract class implements
ListAdapter and SpinnerAdatper interfaces
• ArrayAdapter and CursorAdapter classes are
subclasses of BaseAdapter class
AdapterView
Responsibilities
AdapterView Responsibilities
• Two main responsibilities of AdapterView
> Filling the layout with data (with a help from
Adapter)
> Handling user selections - when a user selects an
item, perform some action
Filling the Layout with Data
• Inserting data into the layout is typically done
by binding the AdapterView class to an Adapter,
which retrieves data from an external source
(perhaps a list that the code supplies or query
results from the device's database).
Handling User Selections
• You handle the user's selection by setting the
class's AdapterView.OnItemClickListener
member to a listener and catching the selection
changes
// Create a message handling object as an anonymous class.
private OnItemClickListener mMessageClickedHandler = new
OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id){
// Display a messagebox.
Toast.makeText(mContext,"You've got an event",
Toast.LENGTH_SHORT).show();
}
};
// Now hook into our object and set its onItemClickListener member
// to our class handler object.
mHistoryView = (ListView)findViewById(R.id.my_list_view);
mHistoryView.setOnItemClickListener(mMessageClickedHandler);
ListActivity class, ListView,
ListAdapter
(ArrayAdapter, CursorAdapter)
ListActivity Activity class
• Android-provided class specially designed for
displaying a list of items by binding to a data
source such as an array or Cursor, and exposes
event handlers when the user selects an item.
> ListActivity hosts a ListView object that can be
bound through an adatper to different data
sources, typically either an array or a Cursor
holding query results.
> setListAdapter(ListAdatper adapter) method
automatically creates ListView object from the
ListAdapter object
• Has a default layout that consists of a single,
full-screen list in the center of the screen
Example of ListActivity Class
public class HelloListView extends ListActivity {
static final String[] COUNTRIES = new String[] {
"Yemen", "Yugoslavia", "Zambia", "Zimbabwe"
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create an adapter
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
this, // Application context
R.layout.list_item, // layout description for each list item
COUNTRIES); // String array of countries defined
// Notice that this does not load a layout file for the Activity (which you
// usually do with setContentView(int)). Instead, setListAdapter(ListAdapter)
// automatically adds a ListView to fill the entire screen of the ListActivity.
setListAdapter(arrayAdapter);
}
}
Spinner, SpinnerAdapter
Spinner
• A view that displays one child at a time and lets
the user pick among them.
• The items in the Spinner come from the
Adapter associated with this view.
Example of Spinner
public class HelloSpinner extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter =
ArrayAdapter.createFromResource(
this,
R.array.planets_array,
android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
}
Example of Spinner Layout
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="10dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:layout_marginBottom="10dip"
android:text="@string/planet_prompt"
/>
<Spinner
android:id="@+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:prompt="@string/planet_prompt"
/>
</LinearLayout>
Thank you

More Related Content

What's hot (15)

ODP
DynamicRecord Presentation
linoj
 
PDF
List Views
Ahsanul Karim
 
PDF
Android L02 - Activities and Adapters
Mohammad Shaker
 
PDF
Day 8: Dealing with Lists and ListViews
Ahsanul Karim
 
PPTX
Report cs3 pillos
paul gonzaga
 
PPTX
Android styles and themes
Sourabh Sahu
 
ODP
Android App Development - 04 Views and layouts
Diego Grancini
 
PPTX
AdRotator and AdRepeater Control in Asp.Net for Msc CS
Thanveen
 
PPT
Android ListView and Custom ListView
Sourabh Sahu
 
PDF
Java Generics wildcards
Kohei Nozaki
 
DOCX
Animal Project
Ahmed Ali
 
PDF
Search syntax training
DavidPixton
 
PPT
2310 b xc
Krazy Koder
 
DOCX
C# Collection classes
MohitKumar1985
 
DOCX
as400 built in function-list
aminem_mp
 
DynamicRecord Presentation
linoj
 
List Views
Ahsanul Karim
 
Android L02 - Activities and Adapters
Mohammad Shaker
 
Day 8: Dealing with Lists and ListViews
Ahsanul Karim
 
Report cs3 pillos
paul gonzaga
 
Android styles and themes
Sourabh Sahu
 
Android App Development - 04 Views and layouts
Diego Grancini
 
AdRotator and AdRepeater Control in Asp.Net for Msc CS
Thanveen
 
Android ListView and Custom ListView
Sourabh Sahu
 
Java Generics wildcards
Kohei Nozaki
 
Animal Project
Ahmed Ali
 
Search syntax training
DavidPixton
 
2310 b xc
Krazy Koder
 
C# Collection classes
MohitKumar1985
 
as400 built in function-list
aminem_mp
 

Similar to Android ui adapter (20)

PPTX
List view3
Vishal Dutt
 
PPTX
Binding data with the AdapterView class.pptx
Gowthami476224
 
PPTX
05 content providers - Android
Wingston
 
PPTX
Android Chapter 4 part2 Types of View and View group
VaibhavSarode16
 
PPTX
Android Training Session 1
Shanmugapriya D
 
PDF
Day 8: Dealing with Lists and ListViews
Ahsanul Karim
 
PPT
Starting with Application Coding.ppt
DharmendraSingh655367
 
PDF
Lab2-android
Lilia Sfaxi
 
PPTX
chp 4 UI component hdjdjdduudfinalt.pptx
Good490110
 
PPTX
Adapter and adapter views that are used in android
JinalBhagat2
 
PPTX
Spinners, Adapters & Fragment Communication
CITSimon
 
PPTX
Lecture 4: Android All Kinds of Lists.pptx
Yousef Alamir
 
PPTX
List view1
Vishal Dutt
 
PDF
01 10 - graphical user interface - others
Siva Kumar reddy Vasipally
 
PPTX
Day 5 android app code advancement
FatimaYousif11
 
PPTX
Android course Lesson2
3sidedcube
 
PDF
Android development - ListView & Adapter
Lope Emano
 
PPTX
B2. activity and intent
PERKYTORIALS
 
PDF
Adapter and cache technique
Hoang Vy Nguyen
 
PPTX
6.Binding Views and Handling Actions.pptx
HongNghaHipK17HL
 
List view3
Vishal Dutt
 
Binding data with the AdapterView class.pptx
Gowthami476224
 
05 content providers - Android
Wingston
 
Android Chapter 4 part2 Types of View and View group
VaibhavSarode16
 
Android Training Session 1
Shanmugapriya D
 
Day 8: Dealing with Lists and ListViews
Ahsanul Karim
 
Starting with Application Coding.ppt
DharmendraSingh655367
 
Lab2-android
Lilia Sfaxi
 
chp 4 UI component hdjdjdduudfinalt.pptx
Good490110
 
Adapter and adapter views that are used in android
JinalBhagat2
 
Spinners, Adapters & Fragment Communication
CITSimon
 
Lecture 4: Android All Kinds of Lists.pptx
Yousef Alamir
 
List view1
Vishal Dutt
 
01 10 - graphical user interface - others
Siva Kumar reddy Vasipally
 
Day 5 android app code advancement
FatimaYousif11
 
Android course Lesson2
3sidedcube
 
Android development - ListView & Adapter
Lope Emano
 
B2. activity and intent
PERKYTORIALS
 
Adapter and cache technique
Hoang Vy Nguyen
 
6.Binding Views and Handling Actions.pptx
HongNghaHipK17HL
 
Ad

More from Krazy Koder (20)

PPT
2310 b xd
Krazy Koder
 
PPT
2310 b xd
Krazy Koder
 
PPT
2310 b xd
Krazy Koder
 
PPT
2310 b xb
Krazy Koder
 
PPT
2310 b 17
Krazy Koder
 
PPT
2310 b 16
Krazy Koder
 
PPT
2310 b 16
Krazy Koder
 
PPT
2310 b 15
Krazy Koder
 
PPT
2310 b 15
Krazy Koder
 
PPT
2310 b 14
Krazy Koder
 
PPT
2310 b 13
Krazy Koder
 
PPT
2310 b 12
Krazy Koder
 
PPT
2310 b 11
Krazy Koder
 
PPT
2310 b 10
Krazy Koder
 
PPT
2310 b 09
Krazy Koder
 
PPT
2310 b 08
Krazy Koder
 
PPT
2310 b 08
Krazy Koder
 
PPT
2310 b 08
Krazy Koder
 
PPT
2310 b 07
Krazy Koder
 
PPT
2310 b 06
Krazy Koder
 
2310 b xd
Krazy Koder
 
2310 b xd
Krazy Koder
 
2310 b xd
Krazy Koder
 
2310 b xb
Krazy Koder
 
2310 b 17
Krazy Koder
 
2310 b 16
Krazy Koder
 
2310 b 16
Krazy Koder
 
2310 b 15
Krazy Koder
 
2310 b 15
Krazy Koder
 
2310 b 14
Krazy Koder
 
2310 b 13
Krazy Koder
 
2310 b 12
Krazy Koder
 
2310 b 11
Krazy Koder
 
2310 b 10
Krazy Koder
 
2310 b 09
Krazy Koder
 
2310 b 08
Krazy Koder
 
2310 b 08
Krazy Koder
 
2310 b 08
Krazy Koder
 
2310 b 07
Krazy Koder
 
2310 b 06
Krazy Koder
 
Ad

Recently uploaded (20)

PDF
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
PDF
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
PDF
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
PDF
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
PPTX
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
PDF
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PPTX
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
PPTX
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
PDF
Lesson 2 - WATER,pH, BUFFERS, AND ACID-BASE.pdf
marvinnbustamante1
 
PPTX
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
PPTX
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
PPTX
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
PDF
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - GLOBAL SUCCESS - CẢ NĂM - NĂM 2024 (VOCABULARY, ...
Nguyen Thanh Tu Collection
 
PDF
Dimensions of Societal Planning in Commonism
StefanMz
 
PDF
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
PPTX
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
PPTX
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
PDF
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
PPTX
How to Set Maximum Difference Odoo 18 POS
Celine George
 
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
Lesson 2 - WATER,pH, BUFFERS, AND ACID-BASE.pdf
marvinnbustamante1
 
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - GLOBAL SUCCESS - CẢ NĂM - NĂM 2024 (VOCABULARY, ...
Nguyen Thanh Tu Collection
 
Dimensions of Societal Planning in Commonism
StefanMz
 
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
How to Set Maximum Difference Odoo 18 POS
Celine George
 

Android ui adapter

  • 2. Topics • AdapterView & Adapter • AdapterView responsibilities • ListActivity, ListView, and ListAdapter • Spinner, SpinnerAdapter
  • 4. What is AdapterView? • The AdapterView is a ViewGroup subclass whose child Views are determined by an Adapter that binds AdapterView object to data of some type. • Typically you are going to use subsclasses of AdapterView class instead of using it directly • Example subclasses of AdapterView class > ListView > Spinner > Gallery
  • 5. What is an Adapter? • An Adapter object acts as a bridge between an AdapterView and the underlying data for that view. > The Adapter provides access to the data items. • The Adapter is also responsible for making a View for each item in the data set. • Types of Adatpers - they implements ListAdatper interface > ArrayAdatper > CursorAdatper > There are a few more
  • 6. Adapter Class Hierarchy • BaseAdatper abstract class implements ListAdapter and SpinnerAdatper interfaces • ArrayAdapter and CursorAdapter classes are subclasses of BaseAdapter class
  • 8. AdapterView Responsibilities • Two main responsibilities of AdapterView > Filling the layout with data (with a help from Adapter) > Handling user selections - when a user selects an item, perform some action
  • 9. Filling the Layout with Data • Inserting data into the layout is typically done by binding the AdapterView class to an Adapter, which retrieves data from an external source (perhaps a list that the code supplies or query results from the device's database).
  • 10. Handling User Selections • You handle the user's selection by setting the class's AdapterView.OnItemClickListener member to a listener and catching the selection changes // Create a message handling object as an anonymous class. private OnItemClickListener mMessageClickedHandler = new OnItemClickListener() { public void onItemClick(AdapterView parent, View v, int position, long id){ // Display a messagebox. Toast.makeText(mContext,"You've got an event", Toast.LENGTH_SHORT).show(); } }; // Now hook into our object and set its onItemClickListener member // to our class handler object. mHistoryView = (ListView)findViewById(R.id.my_list_view); mHistoryView.setOnItemClickListener(mMessageClickedHandler);
  • 12. ListActivity Activity class • Android-provided class specially designed for displaying a list of items by binding to a data source such as an array or Cursor, and exposes event handlers when the user selects an item. > ListActivity hosts a ListView object that can be bound through an adatper to different data sources, typically either an array or a Cursor holding query results. > setListAdapter(ListAdatper adapter) method automatically creates ListView object from the ListAdapter object • Has a default layout that consists of a single, full-screen list in the center of the screen
  • 13. Example of ListActivity Class public class HelloListView extends ListActivity { static final String[] COUNTRIES = new String[] { "Yemen", "Yugoslavia", "Zambia", "Zimbabwe" }; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Create an adapter ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>( this, // Application context R.layout.list_item, // layout description for each list item COUNTRIES); // String array of countries defined // Notice that this does not load a layout file for the Activity (which you // usually do with setContentView(int)). Instead, setListAdapter(ListAdapter) // automatically adds a ListView to fill the entire screen of the ListActivity. setListAdapter(arrayAdapter); } }
  • 15. Spinner • A view that displays one child at a time and lets the user pick among them. • The items in the Spinner come from the Adapter associated with this view.
  • 16. Example of Spinner public class HelloSpinner extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Spinner spinner = (Spinner) findViewById(R.id.spinner); ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource( this, R.array.planets_array, android.R.layout.simple_spinner_item); adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(adapter); spinner.setOnItemSelectedListener(new MyOnItemSelectedListener()); }
  • 17. Example of Spinner Layout <?xml version="1.0" encoding="UTF-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:padding="10dip" android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="10dip" android:layout_marginBottom="10dip" android:text="@string/planet_prompt" /> <Spinner android:id="@+id/spinner" android:layout_width="fill_parent" android:layout_height="wrap_content" android:prompt="@string/planet_prompt" /> </LinearLayout>