SlideShare ist ein Scribd-Unternehmen logo
1 von 16
Welcome
ANDROID ADAPTERS
What is an adapter in android?
• 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.
What is AdapterView Class?
• The AdapterView is a child class of ViewGroup
> A special kind of container of view objects (list items)
• Typically you are going to use subsclasses of
   AdapterView class instead of using it directly
• Example subclasses of AdapterView class
> ListView
> Spinner
> Gallery
• An AdapterView access the data through Adapter object
   Instead of accessing data directly itself
AdapterView Responsibilities
• Two main responsibilities of AdapterView
1) Filling the layout with data (it received
  through the help of an Adapter)
2)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
Two Choices of Activity Class
• Option #1 - Your activity extends Activity class
> You have to create ListView object yourself from
resource file just like any other View object
• Option #2 - Your activity extends ListActivity class
> ListView object gets created by the ListActivity's
contructor, so you don't need to create it yourself
Option #1 - Extending Activity Class
public class HelloListView extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Since HelloListView extends Activity (instead of ListActivity),
// we have to create ListView object ourselves.
ListView lv =(ListView)findViewById(R.id.listview);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
this, // Application context
R.layout.list_item, // layout description for each list item
COUNTRIES);
lv.setAdapter(arrayAdapter);
}
Example of ListView Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
   xmlns:android="http://schemas.android.com/apk/res/andr
   oid"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="@+id/listview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
Example of List Item Layout
<?xml version="1.0" encoding="UTF-8"?>
<TextView
  xmlns:android="http://schemas.android.com/apk
  /res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16sp"
>
</TextView>
Option #2: ListActivity Activity class
• Android-provided utility 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
Option #2: Extending ListActivity
public class HelloListView extends ListActivity {
// Array as a data source
static final String[] COUNTRIES = new String[] {
"Yemen", "Yugoslavia", "Zambia", "Zimbabwe"
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create an adapter from Array data source object
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);
}
}
Disclaimer: This presentation is prepared by trainees of baabtra as a part of
mentoring program. This is not official document of baabtra – Mentoring
Partner
baabtra – Mentoring Partner is the mentoring division of baabte System Technologies Pvt. Ltd
THANK YOU

Weitere ähnliche Inhalte

Was ist angesagt?

JavaScript - Chapter 12 - Document Object Model
  JavaScript - Chapter 12 - Document Object Model  JavaScript - Chapter 12 - Document Object Model
JavaScript - Chapter 12 - Document Object ModelWebStackAcademy
 
Asp.NET Validation controls
Asp.NET Validation controlsAsp.NET Validation controls
Asp.NET Validation controlsGuddu gupta
 
Java collections concept
Java collections conceptJava collections concept
Java collections conceptkumar gaurav
 
Validation Controls in asp.net
Validation Controls in asp.netValidation Controls in asp.net
Validation Controls in asp.netDeep Patel
 
Collection Framework in java
Collection Framework in javaCollection Framework in java
Collection Framework in javaCPD INDIA
 
class and objects
class and objectsclass and objects
class and objectsPayel Guria
 
Javascript arrays
Javascript arraysJavascript arrays
Javascript arraysHassan Dar
 
Constructor and Types of Constructors
Constructor and Types of ConstructorsConstructor and Types of Constructors
Constructor and Types of ConstructorsDhrumil Panchal
 
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteChapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteTushar B Kute
 
OOP Introduction with java programming language
OOP Introduction with java programming languageOOP Introduction with java programming language
OOP Introduction with java programming languageMd.Al-imran Roton
 
Collections and its types in C# (with examples)
Collections and its types in C# (with examples)Collections and its types in C# (with examples)
Collections and its types in C# (with examples)Aijaz Ali Abro
 

Was ist angesagt? (20)

JavaScript - Chapter 12 - Document Object Model
  JavaScript - Chapter 12 - Document Object Model  JavaScript - Chapter 12 - Document Object Model
JavaScript - Chapter 12 - Document Object Model
 
Asp.NET Validation controls
Asp.NET Validation controlsAsp.NET Validation controls
Asp.NET Validation controls
 
Windowforms controls c#
Windowforms controls c#Windowforms controls c#
Windowforms controls c#
 
Java collections concept
Java collections conceptJava collections concept
Java collections concept
 
Validation Controls in asp.net
Validation Controls in asp.netValidation Controls in asp.net
Validation Controls in asp.net
 
Collection Framework in java
Collection Framework in javaCollection Framework in java
Collection Framework in java
 
Php forms
Php formsPhp forms
Php forms
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 
Vectors in Java
Vectors in JavaVectors in Java
Vectors in Java
 
Java Beans
Java BeansJava Beans
Java Beans
 
class and objects
class and objectsclass and objects
class and objects
 
Java: GUI
Java: GUIJava: GUI
Java: GUI
 
Javascript arrays
Javascript arraysJavascript arrays
Javascript arrays
 
JAVA AWT
JAVA AWTJAVA AWT
JAVA AWT
 
Constructor and Types of Constructors
Constructor and Types of ConstructorsConstructor and Types of Constructors
Constructor and Types of Constructors
 
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteChapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
 
OOP Introduction with java programming language
OOP Introduction with java programming languageOOP Introduction with java programming language
OOP Introduction with java programming language
 
ADO .Net
ADO .Net ADO .Net
ADO .Net
 
Collections and its types in C# (with examples)
Collections and its types in C# (with examples)Collections and its types in C# (with examples)
Collections and its types in C# (with examples)
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 

Ähnlich wie Android adapters

Android ui adapter
Android ui adapterAndroid ui adapter
Android ui adapterKrazy Koder
 
Android Training (AdapterView & Adapter)
Android Training (AdapterView & Adapter)Android Training (AdapterView & Adapter)
Android Training (AdapterView & Adapter)Khaled Anaqwa
 
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 ListViewsAhsanul Karim
 
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.pdfdeepua8
 
ListView RecyclerView.pptx
ListView RecyclerView.pptxListView RecyclerView.pptx
ListView RecyclerView.pptxOmakoiMalang
 
Introduction to Listview in Android
Introduction to Listview in AndroidIntroduction to Listview in Android
Introduction to Listview in Androidtechnoguff
 
Android Training Session 1
Android Training Session 1Android Training Session 1
Android Training Session 1Shanmugapriya D
 
05 content providers - Android
05   content providers - Android05   content providers - Android
05 content providers - AndroidWingston
 
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 ListViewsAhsanul Karim
 
Android App Development - 11 Lists, grids, adapters, dialogs and toasts
Android App Development - 11 Lists, grids, adapters, dialogs and toastsAndroid App Development - 11 Lists, grids, adapters, dialogs and toasts
Android App Development - 11 Lists, grids, adapters, dialogs and toastsDiego Grancini
 
Taming Core Data by Arek Holko, Macoscope
Taming Core Data by Arek Holko, MacoscopeTaming Core Data by Arek Holko, Macoscope
Taming Core Data by Arek Holko, MacoscopeMacoscope
 
Day 5 android app code advancement
Day 5  android app code advancementDay 5  android app code advancement
Day 5 android app code advancementFatimaYousif11
 

Ähnlich wie Android adapters (20)

Android ui adapter
Android ui adapterAndroid ui adapter
Android ui adapter
 
Android Training (AdapterView & Adapter)
Android Training (AdapterView & Adapter)Android Training (AdapterView & Adapter)
Android Training (AdapterView & Adapter)
 
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
 
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
 
ListView RecyclerView.pptx
ListView RecyclerView.pptxListView RecyclerView.pptx
ListView RecyclerView.pptx
 
List Views
List ViewsList Views
List Views
 
Session 2- day 3
Session 2- day 3Session 2- day 3
Session 2- day 3
 
Lab2-android
Lab2-androidLab2-android
Lab2-android
 
Android list view tutorial by Javatechig
Android list view tutorial by JavatechigAndroid list view tutorial by Javatechig
Android list view tutorial by Javatechig
 
Collections
CollectionsCollections
Collections
 
Introduction to Listview in Android
Introduction to Listview in AndroidIntroduction to Listview in Android
Introduction to Listview in Android
 
Android Training Session 1
Android Training Session 1Android Training Session 1
Android Training Session 1
 
List adapter with multiple objects
List adapter with multiple objectsList adapter with multiple objects
List adapter with multiple objects
 
05 content providers - Android
05   content providers - Android05   content providers - Android
05 content providers - Android
 
Hello Android
Hello AndroidHello Android
Hello Android
 
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
 
Android App Development - 11 Lists, grids, adapters, dialogs and toasts
Android App Development - 11 Lists, grids, adapters, dialogs and toastsAndroid App Development - 11 Lists, grids, adapters, dialogs and toasts
Android App Development - 11 Lists, grids, adapters, dialogs and toasts
 
ADO.NET
ADO.NETADO.NET
ADO.NET
 
Taming Core Data by Arek Holko, Macoscope
Taming Core Data by Arek Holko, MacoscopeTaming Core Data by Arek Holko, Macoscope
Taming Core Data by Arek Holko, Macoscope
 
Day 5 android app code advancement
Day 5  android app code advancementDay 5  android app code advancement
Day 5 android app code advancement
 

Mehr von baabtra.com - No. 1 supplier of quality freshers

Mehr von baabtra.com - No. 1 supplier of quality freshers (20)

Agile methodology and scrum development
Agile methodology and scrum developmentAgile methodology and scrum development
Agile methodology and scrum development
 
Best coding practices
Best coding practicesBest coding practices
Best coding practices
 
Core java - baabtra
Core java - baabtraCore java - baabtra
Core java - baabtra
 
Acquiring new skills what you should know
Acquiring new skills   what you should knowAcquiring new skills   what you should know
Acquiring new skills what you should know
 
Baabtra.com programming at school
Baabtra.com programming at schoolBaabtra.com programming at school
Baabtra.com programming at school
 
99LMS for Enterprises - LMS that you will love
99LMS for Enterprises - LMS that you will love 99LMS for Enterprises - LMS that you will love
99LMS for Enterprises - LMS that you will love
 
Php sessions & cookies
Php sessions & cookiesPhp sessions & cookies
Php sessions & cookies
 
Php database connectivity
Php database connectivityPhp database connectivity
Php database connectivity
 
Chapter 6 database normalisation
Chapter 6  database normalisationChapter 6  database normalisation
Chapter 6 database normalisation
 
Chapter 5 transactions and dcl statements
Chapter 5  transactions and dcl statementsChapter 5  transactions and dcl statements
Chapter 5 transactions and dcl statements
 
Chapter 4 functions, views, indexing
Chapter 4  functions, views, indexingChapter 4  functions, views, indexing
Chapter 4 functions, views, indexing
 
Chapter 3 stored procedures
Chapter 3 stored proceduresChapter 3 stored procedures
Chapter 3 stored procedures
 
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
Chapter 2  grouping,scalar and aggergate functions,joins   inner join,outer joinChapter 2  grouping,scalar and aggergate functions,joins   inner join,outer join
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
 
Chapter 1 introduction to sql server
Chapter 1 introduction to sql serverChapter 1 introduction to sql server
Chapter 1 introduction to sql server
 
Chapter 1 introduction to sql server
Chapter 1 introduction to sql serverChapter 1 introduction to sql server
Chapter 1 introduction to sql server
 
Microsoft holo lens
Microsoft holo lensMicrosoft holo lens
Microsoft holo lens
 
Blue brain
Blue brainBlue brain
Blue brain
 
5g
5g5g
5g
 
Aptitude skills baabtra
Aptitude skills baabtraAptitude skills baabtra
Aptitude skills baabtra
 
Gd baabtra
Gd baabtraGd baabtra
Gd baabtra
 

Android adapters

  • 3. What is an adapter in android? • 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.
  • 4. What is AdapterView Class? • The AdapterView is a child class of ViewGroup > A special kind of container of view objects (list items) • Typically you are going to use subsclasses of AdapterView class instead of using it directly • Example subclasses of AdapterView class > ListView > Spinner > Gallery • An AdapterView access the data through Adapter object Instead of accessing data directly itself
  • 5. AdapterView Responsibilities • Two main responsibilities of AdapterView 1) Filling the layout with data (it received through the help of an Adapter) 2)Handling user selections - when a user selects an item, perform some action
  • 6. 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).
  • 7. 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
  • 8.
  • 9. Two Choices of Activity Class • Option #1 - Your activity extends Activity class > You have to create ListView object yourself from resource file just like any other View object • Option #2 - Your activity extends ListActivity class > ListView object gets created by the ListActivity's contructor, so you don't need to create it yourself
  • 10. Option #1 - Extending Activity Class public class HelloListView extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // Since HelloListView extends Activity (instead of ListActivity), // we have to create ListView object ourselves. ListView lv =(ListView)findViewById(R.id.listview); ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>( this, // Application context R.layout.list_item, // layout description for each list item COUNTRIES); lv.setAdapter(arrayAdapter); }
  • 11. Example of ListView Layout <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/andr oid" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <ListView android:id="@+id/listview" android:layout_width="fill_parent" android:layout_height="wrap_content"/> </LinearLayout>
  • 12. Example of List Item Layout <?xml version="1.0" encoding="UTF-8"?> <TextView xmlns:android="http://schemas.android.com/apk /res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="10dp" android:textSize="16sp" > </TextView>
  • 13. Option #2: ListActivity Activity class • Android-provided utility 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
  • 14. Option #2: Extending ListActivity public class HelloListView extends ListActivity { // Array as a data source static final String[] COUNTRIES = new String[] { "Yemen", "Yugoslavia", "Zambia", "Zimbabwe" }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Create an adapter from Array data source object 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. Disclaimer: This presentation is prepared by trainees of baabtra as a part of mentoring program. This is not official document of baabtra – Mentoring Partner baabtra – Mentoring Partner is the mentoring division of baabte System Technologies Pvt. Ltd