SlideShare uma empresa Scribd logo
1 de 18
Baixar para ler offline
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

Mais conteúdo relacionado

Mais procurados

Day 8: Dealing with Lists and ListViews
Day 8: Dealing with Lists and ListViewsDay 8: Dealing with Lists and ListViews
Day 8: Dealing with Lists and ListViews
Ahsanul Karim
 
AdRotator and AdRepeater Control in Asp.Net for Msc CS
AdRotator and AdRepeater Control in Asp.Net for Msc CSAdRotator and AdRepeater Control in Asp.Net for Msc CS
AdRotator and AdRepeater Control in Asp.Net for Msc CS
Thanveen
 

Mais procurados (15)

DynamicRecord Presentation
DynamicRecord PresentationDynamicRecord Presentation
DynamicRecord Presentation
 
List Views
List ViewsList Views
List Views
 
Android L02 - Activities and Adapters
Android L02 - Activities and AdaptersAndroid L02 - Activities and Adapters
Android L02 - Activities and Adapters
 
Day 8: Dealing with Lists and ListViews
Day 8: Dealing with Lists and ListViewsDay 8: Dealing with Lists and ListViews
Day 8: Dealing with Lists and ListViews
 
Report cs3 pillos
Report cs3 pillosReport cs3 pillos
Report cs3 pillos
 
Android styles and themes
Android styles and themesAndroid styles and themes
Android styles and themes
 
Android App Development - 04 Views and layouts
Android App Development - 04 Views and layoutsAndroid App Development - 04 Views and layouts
Android App Development - 04 Views and layouts
 
AdRotator and AdRepeater Control in Asp.Net for Msc CS
AdRotator and AdRepeater Control in Asp.Net for Msc CSAdRotator and AdRepeater Control in Asp.Net for Msc CS
AdRotator and AdRepeater Control in Asp.Net for Msc CS
 
Android ListView and Custom ListView
Android ListView and Custom ListView Android ListView and Custom ListView
Android ListView and Custom ListView
 
Java Generics wildcards
Java Generics wildcardsJava Generics wildcards
Java Generics wildcards
 
Animal Project
Animal Project Animal Project
Animal Project
 
Search syntax training
Search syntax trainingSearch syntax training
Search syntax training
 
2310 b xc
2310 b xc2310 b xc
2310 b xc
 
C# Collection classes
C# Collection classesC# Collection classes
C# Collection classes
 
as400 built in function-list
as400 built in function-listas400 built in function-list
as400 built in function-list
 

Semelhante a Android ui adapter

Grid View- GridView is a ViewGroup that displays items in a two d.pdf
Grid View- GridView is a ViewGroup that displays items in a two d.pdfGrid View- GridView is a ViewGroup that displays items in a two d.pdf
Grid View- GridView is a ViewGroup that displays items in a two d.pdf
deepua8
 
Day 8: Dealing with Lists and ListViews
Day 8: Dealing with Lists and ListViewsDay 8: Dealing with Lists and ListViews
Day 8: Dealing with Lists and ListViews
Ahsanul Karim
 

Semelhante a Android ui adapter (20)

ListView RecyclerView.pptx
ListView RecyclerView.pptxListView RecyclerView.pptx
ListView RecyclerView.pptx
 
Day 5 android app code advancement
Day 5  android app code advancementDay 5  android app code advancement
Day 5 android app code advancement
 
Grid View- GridView is a ViewGroup that displays items in a two d.pdf
Grid View- GridView is a ViewGroup that displays items in a two d.pdfGrid View- GridView is a ViewGroup that displays items in a two d.pdf
Grid View- GridView is a ViewGroup that displays items in a two d.pdf
 
Lab2-android
Lab2-androidLab2-android
Lab2-android
 
Android Training Session 1
Android Training Session 1Android Training Session 1
Android Training Session 1
 
Day 8: Dealing with Lists and ListViews
Day 8: Dealing with Lists and ListViewsDay 8: Dealing with Lists and ListViews
Day 8: Dealing with Lists and ListViews
 
05 content providers - Android
05   content providers - Android05   content providers - Android
05 content providers - Android
 
Session 2- day 3
Session 2- day 3Session 2- day 3
Session 2- day 3
 
Murach: How to transfer data from controllers
Murach: How to transfer data from controllersMurach: How to transfer data from controllers
Murach: How to transfer data from controllers
 
Collections
CollectionsCollections
Collections
 
MDAD 4 - Lists, adapters and recycling
MDAD 4 - Lists, adapters and recyclingMDAD 4 - Lists, adapters and recycling
MDAD 4 - Lists, adapters and recycling
 
Android programming basics
Android programming basicsAndroid programming basics
Android programming basics
 
List view3
List view3List view3
List view3
 
MDAD 5 - Android - Lists, adapters and recycling
MDAD 5 - Android - Lists, adapters and recyclingMDAD 5 - Android - Lists, adapters and recycling
MDAD 5 - Android - Lists, adapters and recycling
 
Adapter and cache technique
Adapter and cache techniqueAdapter and cache technique
Adapter and cache technique
 
How to become an Android dev starting from iOS (and vice versa)
How to become an Android dev starting from iOS (and vice versa)How to become an Android dev starting from iOS (and vice versa)
How to become an Android dev starting from iOS (and vice versa)
 
VISUAL BASIC .net data accesss vii
VISUAL BASIC .net data accesss viiVISUAL BASIC .net data accesss vii
VISUAL BASIC .net data accesss vii
 
ThingMaker in Swift
ThingMaker in SwiftThingMaker in Swift
ThingMaker in Swift
 
Chapter 10
Chapter 10Chapter 10
Chapter 10
 
Android L01 - Warm Up
Android L01 - Warm UpAndroid L01 - Warm Up
Android L01 - Warm Up
 

Mais de Krazy Koder (20)

2310 b xd
2310 b xd2310 b xd
2310 b xd
 
2310 b xd
2310 b xd2310 b xd
2310 b xd
 
2310 b xd
2310 b xd2310 b xd
2310 b xd
 
2310 b xb
2310 b xb2310 b xb
2310 b xb
 
2310 b 17
2310 b 172310 b 17
2310 b 17
 
2310 b 16
2310 b 162310 b 16
2310 b 16
 
2310 b 16
2310 b 162310 b 16
2310 b 16
 
2310 b 15
2310 b 152310 b 15
2310 b 15
 
2310 b 15
2310 b 152310 b 15
2310 b 15
 
2310 b 14
2310 b 142310 b 14
2310 b 14
 
2310 b 13
2310 b 132310 b 13
2310 b 13
 
2310 b 12
2310 b 122310 b 12
2310 b 12
 
2310 b 11
2310 b 112310 b 11
2310 b 11
 
2310 b 10
2310 b 102310 b 10
2310 b 10
 
2310 b 09
2310 b 092310 b 09
2310 b 09
 
2310 b 08
2310 b 082310 b 08
2310 b 08
 
2310 b 08
2310 b 082310 b 08
2310 b 08
 
2310 b 08
2310 b 082310 b 08
2310 b 08
 
2310 b 07
2310 b 072310 b 07
2310 b 07
 
2310 b 06
2310 b 062310 b 06
2310 b 06
 

Último

Último (20)

Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 

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>