SlideShare a Scribd company logo
Android Application
Development
List Activity & List View
List View
●Android provides the view "List View" which is capable of
displaying a scrollable list of items.
●"ListView"gets the data to display via an adapter. An adapter which
must extend "Base Adapter" and is responsible for providing the data
model for the list and for converting the data into the fields of the
list.
●Android has two standard adapters
- Array Adapter
-Cursor Adapter
● "Array Adapter" can handle data based on Arrays or Lists while
"SimpleCursorAdapter" handle database related data. You can
develop your own Adapter by extending these classes or the Base
Adapter class.
List Activity
●You can directly use the "List View" in your layout as any other UI
component. In case your Activity is primary showing a list you can
extend the activity "List Activity" which simplifies the handling of a
"List View".
● "List Activity" extends "Activity" and provides simplified handling of
lists. For example you have a predefine method if someone clicks on
a list element.
●"List Activity" contains a "List Adapter" which is responsible for
managing the data. This adapter must be set in the onCreate()
method of your Activity via the method setListAdapter().
●If the user select in the list a list entry the method onListItemClick()
will be called. This method allows to access the selected element.
List Activity
●Android provides already some default layouts which you can use in
your Adapter, e.g.
-"android.R.layout.simple_list_item1". In case you don't want to use
one of the pre-defined layouts your own layout must have an
element with the id "@android:id/list" which is the ListView.
-You can also use a view with the id "@android:id/empty". This view
is displayed if the list is empty. For example you could display here
an error message.
ListViews and performance
●Displaying a large dataset must be efficiently implemented on a
mobile device. Therefore the ListView only creates views (widget) if
needed and attach them to the view hierarchy.
●The default Adapter implementation for a ListView will recycle
views, e.g. if a row is not displayed anymore it will be recycled and
only its content will change.
● If you implement your own adapter for a view you also should do
this to avoid performance problems.
ListActivity with ArrayAdapter and Android
standard layout
●Create a new Android project "com.basistraining.listactivity" with
the activity "MyList".
●You do not need to change the default layout "main.xml". Create the
following activity.
public class MyList extends ListActivity {
/** Called when the activity is first created. */
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
// Create an array of Strings, that will be put to our ListActivity
String[] names = new String[] { "Bangladesh", "India", "China", "Japan",
"Denmark", "Australia", "Germany", "Indonesia"}
// Create an ArrayAdapter, that will actually make the Strings above
// appear in the ListView
this.setListAdapter(new ArrayAdapter<String>(this,android.R.layout.
simple_list_item_1, names));
}
ListActivity with ArrayAdapter and Android
standard layout
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
// Get the item that was clicked
Object o = this.getListAdapter().getItem(position);
String keyword = o.toString();
Toast.makeText(this, "You selected: " + keyword, Toast.LENGTH_LONG)
.show();
}
ListActivity with ArrayAdapter and Android
standard layout
ListActivity with own layout
●You can also define your own layout for the rows and assign this
layout to your row adapter. We will add a graphic to each list entry.
●Create the following layout file "rowlayout.xml" in the res/layout
folder of your project"com.basistraining.listactivity".
ListActivity with own layout
●Change your activity "MyList" to the following. This is almost the
same coding as in the previous example,
●The only difference is that we are using our own layout in the
ArrayAdapter and telling the adapter which UI element should
contains the text.
ListActivity with own layout
ListActivity with own layout
ListActivities with flexible layout
●The following uses an image "no.png". I placed it in the "res/drawable-
mdpi" folder. You must maintain your own icon. In the easiest case just
copy "icon.png" to "no.png" and use a drawing program to change it a little
bit.
●If you want to influence the display of the different rows you can define
your own adapter and override the getView() method. This method is
responsible for creating the individual rows of your "ListView". getView()
need to return a View (containing several others) for each row.
●For this read the pre-defined layout via the class "LayoutInflator" and
return one individual view per row. We extend ArrayAdapter but we could
also directly implement "BaseAdapter“
●If "convertView" is not null we re-used this view. Android recycles rows
(views) which are not displayed anymore. Using exsting rows saves
memory and CPU consumption.
ListActivities with flexible layout
●Our implementation will also use the so-called "ViewHolder" pattern. The
method findViewById() is a expensive operation, therefore we should
avoid doing this operation if not necessary.
●The ViewHolder stores a reference to the required views in a row. This
ViewHolder is then attached to the row via the method setTag().
●Every view can get a tag assigned. If the row is recycled we can get the
ViewHolder via getTag() method.
●This seems like a lot of overhead but is much faster then the repetitive
call of findViewById().
ListActivities with flexible layout
●We still using the project "com.basistraining.listactivity". Create the
following class "MyArrayAdapter.java".
ListActivities with flexible layout
ListActivities with flexible layout
●Now In the MyList class we write the following code
ListActivities with flexible layout
Rows interacting with the data model
●Your row layout can also contain views which interact with the
underlying data model.
●For example you can have a "Checkbox" view in your row and if the
checkbox is selected you change the data which is displayed in the
row.
●We still use the same project. Create a new row layout
"rowbuttonlayout.xml“.
Rows interacting with the data model
Rows interacting with the data model
●create for this example the class "Model" which hold the name
and the information if this element is currently selected.
Rows interacting with the data model
●Create the following Adapter. This adapter will add a listener on the
Checkbox. If the checkbox is selected the underlying data of the
model is also changed. Search Checkbox gets its model element
assigned via the setTag() method.
Rows interacting with the data model
Rows interacting with the data model
●Finally change your "ListView" to the following.
Rows interacting with the data model
●If you start your app you should be able to flag items. These
changes will be reflected in your model.

More Related Content

What's hot (20)

PPT
Londroid Android Home Screen Widgets
Richard Hyndman
 
PPTX
Android UI
nationalmobileapps
 
PPTX
Create an android app for database creation using.pptx
vishal choudhary
 
PPTX
Day 15: Working in Background
Ahsanul Karim
 
PPTX
Develop a native application that uses GPS location.pptx
vishal choudhary
 
PPTX
Android Workshop: Day 1 Part 3
Ahsanul Karim
 
PDF
Lecture 3 getting active through activities
Ahsanul Karim
 
PPTX
Android Widget
ELLURU Kalyan
 
DOCX
Android App To Display Employee Details
Saikrishna Tanguturu
 
PDF
Marakana Android User Interface
Marko Gargenta
 
PPT
Day 4: Android: Getting Active through Activities
Ahsanul Karim
 
PDF
Android ui layout
Krazy Koder
 
PDF
Lab2-android
Lilia Sfaxi
 
PPT
View groups containers
Mani Selvaraj
 
PPT
android layouts
Deepa Rani
 
PPTX
Android Layout.pptx
vishal choudhary
 
PPTX
Android Intent.pptx
vishal choudhary
 
PPTX
Create an other activity lesson 3
Kalluri Vinay Reddy
 
PPT
Day: 2 Environment Setup for Android Application Development
Ahsanul Karim
 
PPTX
Android Programming.pptx
vishal choudhary
 
Londroid Android Home Screen Widgets
Richard Hyndman
 
Android UI
nationalmobileapps
 
Create an android app for database creation using.pptx
vishal choudhary
 
Day 15: Working in Background
Ahsanul Karim
 
Develop a native application that uses GPS location.pptx
vishal choudhary
 
Android Workshop: Day 1 Part 3
Ahsanul Karim
 
Lecture 3 getting active through activities
Ahsanul Karim
 
Android Widget
ELLURU Kalyan
 
Android App To Display Employee Details
Saikrishna Tanguturu
 
Marakana Android User Interface
Marko Gargenta
 
Day 4: Android: Getting Active through Activities
Ahsanul Karim
 
Android ui layout
Krazy Koder
 
Lab2-android
Lilia Sfaxi
 
View groups containers
Mani Selvaraj
 
android layouts
Deepa Rani
 
Android Layout.pptx
vishal choudhary
 
Android Intent.pptx
vishal choudhary
 
Create an other activity lesson 3
Kalluri Vinay Reddy
 
Day: 2 Environment Setup for Android Application Development
Ahsanul Karim
 
Android Programming.pptx
vishal choudhary
 

Viewers also liked (17)

PPTX
Day 9: Make Your App Location Aware using Location API
Ahsanul Karim
 
PPTX
Android Services
Ahsanul Karim
 
PDF
Sensors in Android (old)
Ahsanul Karim
 
PPTX
Android GPS Tutorial
Ahsanul Karim
 
PPTX
Day: 1 Introduction to Mobile Application Development (in Android)
Ahsanul Karim
 
PPT
Day 6: Android BroadcastReceiver Component
Ahsanul Karim
 
PDF
Day 2 android internals a quick overview
Ahsanul Karim
 
PPTX
Creating the first app with android studio
Parinita03
 
PDF
Training android
University of Technology
 
PPTX
Mcq peresentation
Shah Jalal Hridoy
 
PPT
Day 5: Android User Interface [View Widgets]
Ahsanul Karim
 
PPTX
Android 1.8 sensor
Ahsanul Karim
 
PPTX
Action Bar Sherlock tutorial
Ahsanul Karim
 
PDF
Day1 before getting_started
Ahsanul Karim
 
PPTX
Android Workshop Day 1 Part 2
Ahsanul Karim
 
PDF
AndroidManifest
Ahsanul Karim
 
PDF
GCM for Android
Ahsanul Karim
 
Day 9: Make Your App Location Aware using Location API
Ahsanul Karim
 
Android Services
Ahsanul Karim
 
Sensors in Android (old)
Ahsanul Karim
 
Android GPS Tutorial
Ahsanul Karim
 
Day: 1 Introduction to Mobile Application Development (in Android)
Ahsanul Karim
 
Day 6: Android BroadcastReceiver Component
Ahsanul Karim
 
Day 2 android internals a quick overview
Ahsanul Karim
 
Creating the first app with android studio
Parinita03
 
Training android
University of Technology
 
Mcq peresentation
Shah Jalal Hridoy
 
Day 5: Android User Interface [View Widgets]
Ahsanul Karim
 
Android 1.8 sensor
Ahsanul Karim
 
Action Bar Sherlock tutorial
Ahsanul Karim
 
Day1 before getting_started
Ahsanul Karim
 
Android Workshop Day 1 Part 2
Ahsanul Karim
 
AndroidManifest
Ahsanul Karim
 
GCM for Android
Ahsanul Karim
 
Ad

Similar to Day 8: Dealing with Lists and ListViews (20)

PDF
Day 8: Dealing with Lists and ListViews
Ahsanul Karim
 
DOCX
Android list view tutorial by Javatechig
Javatechig Resources for Developers
 
PPTX
chp 4 UI component hdjdjdduudfinalt.pptx
Good490110
 
PPTX
MDAD 5 - Android - Lists, adapters and recycling
Alexandru Radovici
 
PPT
Android ListView and Custom ListView
Sourabh Sahu
 
PPTX
MDAD 4 - Lists, adapters and recycling
Alexandru Radovici
 
PPTX
Introduction to Listview in Android
technoguff
 
PPTX
ListView and Custom ListView on Android Development [Thai]
Somkiat Khitwongwattana
 
PPTX
List adapter with multiple objects
baabtra.com - No. 1 supplier of quality freshers
 
PDF
Android - Build User Interface
MingHo Chang
 
ODP
Android App Development - 11 Lists, grids, adapters, dialogs and toasts
Diego Grancini
 
PPTX
Android Chapter 4 part2 Types of View and View group
VaibhavSarode16
 
PPTX
Android Training (AdapterView & Adapter)
Khaled Anaqwa
 
PPTX
layout and UI.pptx
debasish duarah
 
DOCX
Leture5 exercise onactivities
maamir farooq
 
DOCX
Lecture exercise on activities
maamir farooq
 
PPTX
Android course Lesson2
3sidedcube
 
PPT
Hello Android
Trong Dinh
 
PPTX
05 content providers - Android
Wingston
 
Day 8: Dealing with Lists and ListViews
Ahsanul Karim
 
Android list view tutorial by Javatechig
Javatechig Resources for Developers
 
chp 4 UI component hdjdjdduudfinalt.pptx
Good490110
 
MDAD 5 - Android - Lists, adapters and recycling
Alexandru Radovici
 
Android ListView and Custom ListView
Sourabh Sahu
 
MDAD 4 - Lists, adapters and recycling
Alexandru Radovici
 
Introduction to Listview in Android
technoguff
 
ListView and Custom ListView on Android Development [Thai]
Somkiat Khitwongwattana
 
List adapter with multiple objects
baabtra.com - No. 1 supplier of quality freshers
 
Android - Build User Interface
MingHo Chang
 
Android App Development - 11 Lists, grids, adapters, dialogs and toasts
Diego Grancini
 
Android Chapter 4 part2 Types of View and View group
VaibhavSarode16
 
Android Training (AdapterView & Adapter)
Khaled Anaqwa
 
layout and UI.pptx
debasish duarah
 
Leture5 exercise onactivities
maamir farooq
 
Lecture exercise on activities
maamir farooq
 
Android course Lesson2
3sidedcube
 
Hello Android
Trong Dinh
 
05 content providers - Android
Wingston
 
Ad

More from Ahsanul Karim (11)

PDF
Lecture 5: Storage: Saving Data Database, Files & Preferences
Ahsanul Karim
 
PDF
Lecture 2(b) Android Internals A Quick Overview
Ahsanul Karim
 
PDF
Lecture 1 Session 1 Before Getting Started
Ahsanul Karim
 
PDF
লেকচার ১ (ক)- শুরুর আগে:
Ahsanul Karim
 
DOC
Day 4: Activity lifecycle
Ahsanul Karim
 
PDF
Day 1 Android: Before Getting Started
Ahsanul Karim
 
PDF
Mobile Banking in Bangladesh: An Incomplete Study
Ahsanul Karim
 
PDF
Ui layout (incomplete)
Ahsanul Karim
 
PPTX
Android before getting started
Ahsanul Karim
 
PPTX
Introduction to Android Development: Before Getting Started
Ahsanul Karim
 
PPTX
Client-Server
Ahsanul Karim
 
Lecture 5: Storage: Saving Data Database, Files & Preferences
Ahsanul Karim
 
Lecture 2(b) Android Internals A Quick Overview
Ahsanul Karim
 
Lecture 1 Session 1 Before Getting Started
Ahsanul Karim
 
লেকচার ১ (ক)- শুরুর আগে:
Ahsanul Karim
 
Day 4: Activity lifecycle
Ahsanul Karim
 
Day 1 Android: Before Getting Started
Ahsanul Karim
 
Mobile Banking in Bangladesh: An Incomplete Study
Ahsanul Karim
 
Ui layout (incomplete)
Ahsanul Karim
 
Android before getting started
Ahsanul Karim
 
Introduction to Android Development: Before Getting Started
Ahsanul Karim
 
Client-Server
Ahsanul Karim
 

Recently uploaded (20)

PDF
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
PPTX
AI in Daily Life: How Artificial Intelligence Helps Us Every Day
vanshrpatil7
 
PPTX
Agentic AI in Healthcare Driving the Next Wave of Digital Transformation
danielle hunter
 
PPTX
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
PDF
TrustArc Webinar - Navigating Data Privacy in LATAM: Laws, Trends, and Compli...
TrustArc
 
PDF
Make GenAI investments go further with the Dell AI Factory
Principled Technologies
 
PDF
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
PPTX
AI Code Generation Risks (Ramkumar Dilli, CIO, Myridius)
Priyanka Aash
 
PDF
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
PPTX
The Future of AI & Machine Learning.pptx
pritsen4700
 
PPTX
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
PPTX
Introduction to Flutter by Ayush Desai.pptx
ayushdesai204
 
PPTX
Agile Chennai 18-19 July 2025 | Emerging patterns in Agentic AI by Bharani Su...
AgileNetwork
 
PDF
The Future of Mobile Is Context-Aware—Are You Ready?
iProgrammer Solutions Private Limited
 
PDF
RAT Builders - How to Catch Them All [DeepSec 2024]
malmoeb
 
PDF
OFFOFFBOX™ – A New Era for African Film | Startup Presentation
ambaicciwalkerbrian
 
PPTX
Agile Chennai 18-19 July 2025 | Workshop - Enhancing Agile Collaboration with...
AgileNetwork
 
PDF
State-Dependent Conformal Perception Bounds for Neuro-Symbolic Verification
Ivan Ruchkin
 
PPTX
Farrell_Programming Logic and Design slides_10e_ch02_PowerPoint.pptx
bashnahara11
 
PDF
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
AI in Daily Life: How Artificial Intelligence Helps Us Every Day
vanshrpatil7
 
Agentic AI in Healthcare Driving the Next Wave of Digital Transformation
danielle hunter
 
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
TrustArc Webinar - Navigating Data Privacy in LATAM: Laws, Trends, and Compli...
TrustArc
 
Make GenAI investments go further with the Dell AI Factory
Principled Technologies
 
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
AI Code Generation Risks (Ramkumar Dilli, CIO, Myridius)
Priyanka Aash
 
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
The Future of AI & Machine Learning.pptx
pritsen4700
 
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
Introduction to Flutter by Ayush Desai.pptx
ayushdesai204
 
Agile Chennai 18-19 July 2025 | Emerging patterns in Agentic AI by Bharani Su...
AgileNetwork
 
The Future of Mobile Is Context-Aware—Are You Ready?
iProgrammer Solutions Private Limited
 
RAT Builders - How to Catch Them All [DeepSec 2024]
malmoeb
 
OFFOFFBOX™ – A New Era for African Film | Startup Presentation
ambaicciwalkerbrian
 
Agile Chennai 18-19 July 2025 | Workshop - Enhancing Agile Collaboration with...
AgileNetwork
 
State-Dependent Conformal Perception Bounds for Neuro-Symbolic Verification
Ivan Ruchkin
 
Farrell_Programming Logic and Design slides_10e_ch02_PowerPoint.pptx
bashnahara11
 
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 

Day 8: Dealing with Lists and ListViews

  • 2. List View ●Android provides the view "List View" which is capable of displaying a scrollable list of items. ●"ListView"gets the data to display via an adapter. An adapter which must extend "Base Adapter" and is responsible for providing the data model for the list and for converting the data into the fields of the list. ●Android has two standard adapters - Array Adapter -Cursor Adapter ● "Array Adapter" can handle data based on Arrays or Lists while "SimpleCursorAdapter" handle database related data. You can develop your own Adapter by extending these classes or the Base Adapter class.
  • 3. List Activity ●You can directly use the "List View" in your layout as any other UI component. In case your Activity is primary showing a list you can extend the activity "List Activity" which simplifies the handling of a "List View". ● "List Activity" extends "Activity" and provides simplified handling of lists. For example you have a predefine method if someone clicks on a list element. ●"List Activity" contains a "List Adapter" which is responsible for managing the data. This adapter must be set in the onCreate() method of your Activity via the method setListAdapter(). ●If the user select in the list a list entry the method onListItemClick() will be called. This method allows to access the selected element.
  • 4. List Activity ●Android provides already some default layouts which you can use in your Adapter, e.g. -"android.R.layout.simple_list_item1". In case you don't want to use one of the pre-defined layouts your own layout must have an element with the id "@android:id/list" which is the ListView. -You can also use a view with the id "@android:id/empty". This view is displayed if the list is empty. For example you could display here an error message.
  • 5. ListViews and performance ●Displaying a large dataset must be efficiently implemented on a mobile device. Therefore the ListView only creates views (widget) if needed and attach them to the view hierarchy. ●The default Adapter implementation for a ListView will recycle views, e.g. if a row is not displayed anymore it will be recycled and only its content will change. ● If you implement your own adapter for a view you also should do this to avoid performance problems.
  • 6. ListActivity with ArrayAdapter and Android standard layout ●Create a new Android project "com.basistraining.listactivity" with the activity "MyList". ●You do not need to change the default layout "main.xml". Create the following activity. public class MyList extends ListActivity { /** Called when the activity is first created. */ public void onCreate(Bundle icicle) { super.onCreate(icicle); // Create an array of Strings, that will be put to our ListActivity String[] names = new String[] { "Bangladesh", "India", "China", "Japan", "Denmark", "Australia", "Germany", "Indonesia"} // Create an ArrayAdapter, that will actually make the Strings above // appear in the ListView this.setListAdapter(new ArrayAdapter<String>(this,android.R.layout. simple_list_item_1, names)); }
  • 7. ListActivity with ArrayAdapter and Android standard layout @Override protected void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id); // Get the item that was clicked Object o = this.getListAdapter().getItem(position); String keyword = o.toString(); Toast.makeText(this, "You selected: " + keyword, Toast.LENGTH_LONG) .show(); }
  • 8. ListActivity with ArrayAdapter and Android standard layout
  • 9. ListActivity with own layout ●You can also define your own layout for the rows and assign this layout to your row adapter. We will add a graphic to each list entry. ●Create the following layout file "rowlayout.xml" in the res/layout folder of your project"com.basistraining.listactivity".
  • 10. ListActivity with own layout ●Change your activity "MyList" to the following. This is almost the same coding as in the previous example, ●The only difference is that we are using our own layout in the ArrayAdapter and telling the adapter which UI element should contains the text.
  • 13. ListActivities with flexible layout ●The following uses an image "no.png". I placed it in the "res/drawable- mdpi" folder. You must maintain your own icon. In the easiest case just copy "icon.png" to "no.png" and use a drawing program to change it a little bit. ●If you want to influence the display of the different rows you can define your own adapter and override the getView() method. This method is responsible for creating the individual rows of your "ListView". getView() need to return a View (containing several others) for each row. ●For this read the pre-defined layout via the class "LayoutInflator" and return one individual view per row. We extend ArrayAdapter but we could also directly implement "BaseAdapter“ ●If "convertView" is not null we re-used this view. Android recycles rows (views) which are not displayed anymore. Using exsting rows saves memory and CPU consumption.
  • 14. ListActivities with flexible layout ●Our implementation will also use the so-called "ViewHolder" pattern. The method findViewById() is a expensive operation, therefore we should avoid doing this operation if not necessary. ●The ViewHolder stores a reference to the required views in a row. This ViewHolder is then attached to the row via the method setTag(). ●Every view can get a tag assigned. If the row is recycled we can get the ViewHolder via getTag() method. ●This seems like a lot of overhead but is much faster then the repetitive call of findViewById().
  • 15. ListActivities with flexible layout ●We still using the project "com.basistraining.listactivity". Create the following class "MyArrayAdapter.java".
  • 17. ListActivities with flexible layout ●Now In the MyList class we write the following code
  • 19. Rows interacting with the data model ●Your row layout can also contain views which interact with the underlying data model. ●For example you can have a "Checkbox" view in your row and if the checkbox is selected you change the data which is displayed in the row. ●We still use the same project. Create a new row layout "rowbuttonlayout.xml“.
  • 20. Rows interacting with the data model
  • 21. Rows interacting with the data model ●create for this example the class "Model" which hold the name and the information if this element is currently selected.
  • 22. Rows interacting with the data model ●Create the following Adapter. This adapter will add a listener on the Checkbox. If the checkbox is selected the underlying data of the model is also changed. Search Checkbox gets its model element assigned via the setTag() method.
  • 23. Rows interacting with the data model
  • 24. Rows interacting with the data model ●Finally change your "ListView" to the following.
  • 25. Rows interacting with the data model ●If you start your app you should be able to flag items. These changes will be reflected in your model.