SlideShare una empresa de Scribd logo
1 de 15
Android
ListView
Perfect APK
Android ListView Tutorial
Almost every Android app contains at least one ListView. ListView is one of several ways of displaying a collection of
items, and in many cases it is the most appropriate, especially in cases where the items should appear in a
specific order, or where text is displayed. In other cases you might want to consider other types of views, such as
a GridView.
In this tutorial you learn how to properly use a ListView in your Android apps, by following a ListView example for
understanding the building blocks, patterns, and best practices. Please note that the icon set for this example was
created by Design Deck.
Building Blocks
Every ListView requires the following elements, although some of the them may be hidden by using default components
provided in the Android SDK:
● Item Layout File - a ListView item XML layout file to be used by the list adapter. Sometimes layout files from the
Android SDK such as simple_list_item_1 and simple_list_item_2 are used. In this tutorial we will be using a layout
that contains an ImageView for the icon, and TextViews for the title and the description.
● Parent View Layout File - the layout file for the activity or fragment. In this tutorial this element is hidden by using the
default view of the ListFragment. For information about using a custom layout with a ListActivity or ListFragment read
Custom layout in ListActivity and ListFragment.
● Data Container - used for holding the data of a single ListView item. May be as simple as a String or a complex class.
In this tutorial we will be using a class that includes an icon, a title and a description.
● List Adapter - used for creating the views of the ListView items. In this tutorial we will be using a custom list adapter
that extends the ArrayAdapter class.
● Activity or Fragment - the ListView can be used directly or by using a ListActivity or ListFragment which are
Items Layout File
The item layout file is used by the list adapter for creating the row view. Below is an example of a ListView row:
The row view is described in the res/layout/listview_item.xml file below:
1. <?xml version="1.0" encoding="utf-8"?>
2. <!-- the parent view - provides the gray background -->
3. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
4. android:layout_width="match_parent"
5. android:layout_height="match_parent"
6. android:layout_gravity="center"
7. android:gravity="center_vertical"
8. android:background="@color/frame_background"
9. android:padding="5dp" >
Items Layout File
1.
2. <!-- the innner view - provides the white rectangle -->
3. <RelativeLayout android:layout_width="fill_parent"
4. android:layout_height="wrap_content"
5. android:background="@drawable/frame" >
6.
7. <!-- the icon view -->
8. <ImageView android:id="@+id/ivIcon"
9. android:layout_width="wrap_content"
10. android:layout_height="wrap_content"
11. android:padding="5dp"
12. android:contentDescription="@string/icon_content_description"
13. android:scaleType="fitXY"
14. android:layout_alignParentLeft="true" />
Items Layout File
1.
2. <!-- the container view for the title and description -->
3. <RelativeLayout android:layout_width="wrap_content"
4. android:layout_height="wrap_content"
5. android:layout_toRightOf="@id/ivIcon"
6. android:layout_centerVertical="true" >
7.
8. <!-- the title view -->
9. <TextView android:id="@+id/tvTitle"
10. android:layout_width="wrap_content"
11. android:layout_height="wrap_content"
12. android:textAppearance="@android:style/TextAppearance.Medium" />
Items Layout File
1. <!-- the description view -->
2. <TextView android:id="@+id/tvDescription"
3. android:layout_below="@id/tvTitle"
4. android:layout_width="wrap_content"
5. android:layout_height="wrap_content"
6. android:textAppearance="@android:style/TextAppearance.Small" />
7. </RelativeLayout>
8.
9. </RelativeLayout>
10.
11. </RelativeLayout>
Items Layout File
The layout colors for this example are defined in the res/values/colors.xml file below:
1. <?xml version="1.0" encoding="utf-8"?>
2. <resources>
3. <color name="frame">#b4b4b4</color>
4. <color name="frame_solid">#eee</color>
5. <color name="frame_background">#bcbcbc</color>
6. </resources>
The rectangular frame in this example is defined in the res/drawable/frame.xml file below:
1. <?xml version="1.0" encoding="utf-8"?>
2. <shape xmlns:android="http://schemas.android.com/apk/res/android"
3. android:shape="rectangle" >
4.
5. <corners android:radius="2dp" />
6. <stroke android:width="1dp" android:color="@color/frame" />
7. <solid android:color="@color/frame_solid"/>
8. </shape>
Parent View Layout File
In this example I used the default view of the ListFragment. For information
about using a custom layout with a ListActivity or ListFragment read Custom
layout in ListActivity and ListFragment.
Data Container
The ListViewItem class below is used for holding the data of the ListView item:
1. public class ListViewItem {
2. public final Drawable icon; // the drawable for the ListView item ImageView
3. public final String title; // the text for the ListView item title
4. public final String description; // the text for the ListView item description
5.
6. public ListViewItem(Drawable icon, String title, String description) {
7. this.icon = icon;
8. this.title = title;
9. this.description = description;
10. }
List Adapter
The list adapter class in this example extends the ArrayAdpter class. It overrides the getView() method of the ArrayAdapter in which the row
view is created, and uses the View Holder pattern which prevents using findViewById() repeatedly. Below is the ListViewDemoAdapter
class used in this example:
1. public class ListViewDemoAdapter extends ArrayAdapter<ListViewItem> {
2.
3. public ListViewDemoAdapter(Context context, List<ListViewItem> items) {
4. super(context, R.layout.listview_item, items);
5. }
List Adapter
1. @Override
2. public View getView(int position, View convertView, ViewGroup parent) {
3. ViewHolder viewHolder;
4.
5. if(convertView == null) {
6. // inflate the GridView item layout
7. LayoutInflater inflater = LayoutInflater.from(getContext());
8. convertView = inflater.inflate(R.layout.listview_item, parent, false);
9.
10. // initialize the view holder
11. viewHolder = new ViewHolder();
12. viewHolder.ivIcon = (ImageView) convertView.findViewById(R.id.ivIcon);
13. viewHolder.tvTitle = (TextView) convertView.findViewById(R.id.tvTitle);
14. viewHolder.tvDescription = (TextView) convertView.findViewById(R.id.tvDescription);
15. convertView.setTag(viewHolder);
16. } else {
17. // recycle the already inflated view
18. viewHolder = (ViewHolder) convertView.getTag();
19. }
20.
21. // update the item view
22. ListViewItem item = getItem(position);
23. viewHolder.ivIcon.setImageDrawable(item.icon);
24. viewHolder.tvTitle.setText(item.title);
25. viewHolder.tvDescription.setText(item.description);
26.
27. return convertView;
28. }
List Adapter
1. /**
2. * The view holder design pattern prevents using findViewById()
3. * repeatedly in the getView() method of the adapter.
4. *
5. * @see http://developer.android.com/training/improving-layouts/smooth-
scrolling.html#ViewHolder
6. */
7. private static class ViewHolder {
8. ImageView ivIcon;
9. TextView tvTitle;
10. TextView tvDescription;
11. }
Activity or Fragment
In this example a ListFragment is used for displaying the ListView. Using a ListActivity or ListFragment instead of a simple Activity or
Fragment saves a lot of boilerplate code such as setting the ListView, implementing the OnClickListener interface, and other code
encapsulated in convenience methods such as setListAdaper() and getListView(). Below is the ListViewDemoAdapter class used in this
example:
1. public class ListViewDemoFragment extends ListFragment {
2. private List<ListViewItem> mItems; // ListView items list
3.
4. @Override
5. public void onCreate(Bundle savedInstanceState) {
6. super.onCreate(savedInstanceState);
7.
8. // initialize the items list
9. mItems = new ArrayList<ListViewItem>();
10. Resources resources = getResources();
Activity or Fragment
1. @Override
2. public void onViewCreated(View view, Bundle savedInstanceState) {
3. super.onViewCreated(view, savedInstanceState);
4. // remove the default dividers from the ListView of the ListFragment
5. getListView().setDivider(null);
6. }
7.
8. @Override
9. public void onListItemClick(ListView l, View v, int position, long id) {
10. // retrieve theListView item
11. ListViewItem item = mItems.get(position);
12.
13. // do something
14. Toast.makeText(getActivity(), item.title, Toast.LENGTH_SHORT).show();
15. }
16. }

Más contenido relacionado

Destacado

Android Radio streaming
Android Radio streamingAndroid Radio streaming
Android Radio streamingAgus Haryanto
 
Android Dialogs Tutorial
Android Dialogs TutorialAndroid Dialogs Tutorial
Android Dialogs TutorialPerfect APK
 
android app development training report
android app development training reportandroid app development training report
android app development training reportRishita Jaggi
 
Data Handning with Sqlite for Android
Data Handning with Sqlite for AndroidData Handning with Sqlite for Android
Data Handning with Sqlite for AndroidJakir Hossain
 
(続) Effective SQLite for Android
(続) Effective SQLite for Android(続) Effective SQLite for Android
(続) Effective SQLite for AndroidShinobu Okano
 
Android Fast Track - Database SQLite (Kamus Tiga Bahasa)
Android Fast Track - Database SQLite (Kamus Tiga Bahasa)Android Fast Track - Database SQLite (Kamus Tiga Bahasa)
Android Fast Track - Database SQLite (Kamus Tiga Bahasa)Agus Haryanto
 
Android Sliding Menu dengan Navigation Drawer
Android Sliding Menu dengan Navigation DrawerAndroid Sliding Menu dengan Navigation Drawer
Android Sliding Menu dengan Navigation DrawerAgus Haryanto
 
Belajar Android Studio Memberi Efek animasi pada Button
Belajar Android Studio Memberi Efek animasi pada ButtonBelajar Android Studio Memberi Efek animasi pada Button
Belajar Android Studio Memberi Efek animasi pada ButtonAgus Haryanto
 
Kenalan Dengan Firebase Android
Kenalan Dengan Firebase AndroidKenalan Dengan Firebase Android
Kenalan Dengan Firebase AndroidAgus Haryanto
 
Tutorial Android Membuat Aplikasi senter Flash light
Tutorial Android Membuat Aplikasi senter Flash lightTutorial Android Membuat Aplikasi senter Flash light
Tutorial Android Membuat Aplikasi senter Flash lightAgus Haryanto
 
Android Fast Track CRUD Android PHP MySql
Android Fast Track CRUD Android PHP MySqlAndroid Fast Track CRUD Android PHP MySql
Android Fast Track CRUD Android PHP MySqlAgus Haryanto
 
Belajar Android PHP MySQL Login dengan Volley
Belajar Android PHP MySQL Login dengan VolleyBelajar Android PHP MySQL Login dengan Volley
Belajar Android PHP MySQL Login dengan VolleyAgus Haryanto
 
Adapter & ListView & ExpandalbeListView
Adapter & ListView & ExpandalbeListViewAdapter & ListView & ExpandalbeListView
Adapter & ListView & ExpandalbeListViewYuki Anzai
 
Belajar Android Membuat Katalog Produk
Belajar Android Membuat Katalog ProdukBelajar Android Membuat Katalog Produk
Belajar Android Membuat Katalog ProdukAgus Haryanto
 
Android development - ListView & Adapter
Android development - ListView & AdapterAndroid development - ListView & Adapter
Android development - ListView & AdapterLope Emano
 
Belajar Android Studio CRUD Data Mahasiswa
Belajar Android Studio CRUD Data MahasiswaBelajar Android Studio CRUD Data Mahasiswa
Belajar Android Studio CRUD Data MahasiswaAgus Haryanto
 
Introduction to Listview in Android
Introduction to Listview in AndroidIntroduction to Listview in Android
Introduction to Listview in Androidtechnoguff
 
ListView and Custom ListView on Android Development [Thai]
ListView and Custom ListView on Android Development [Thai]ListView and Custom ListView on Android Development [Thai]
ListView and Custom ListView on Android Development [Thai]Somkiat Khitwongwattana
 

Destacado (18)

Android Radio streaming
Android Radio streamingAndroid Radio streaming
Android Radio streaming
 
Android Dialogs Tutorial
Android Dialogs TutorialAndroid Dialogs Tutorial
Android Dialogs Tutorial
 
android app development training report
android app development training reportandroid app development training report
android app development training report
 
Data Handning with Sqlite for Android
Data Handning with Sqlite for AndroidData Handning with Sqlite for Android
Data Handning with Sqlite for Android
 
(続) Effective SQLite for Android
(続) Effective SQLite for Android(続) Effective SQLite for Android
(続) Effective SQLite for Android
 
Android Fast Track - Database SQLite (Kamus Tiga Bahasa)
Android Fast Track - Database SQLite (Kamus Tiga Bahasa)Android Fast Track - Database SQLite (Kamus Tiga Bahasa)
Android Fast Track - Database SQLite (Kamus Tiga Bahasa)
 
Android Sliding Menu dengan Navigation Drawer
Android Sliding Menu dengan Navigation DrawerAndroid Sliding Menu dengan Navigation Drawer
Android Sliding Menu dengan Navigation Drawer
 
Belajar Android Studio Memberi Efek animasi pada Button
Belajar Android Studio Memberi Efek animasi pada ButtonBelajar Android Studio Memberi Efek animasi pada Button
Belajar Android Studio Memberi Efek animasi pada Button
 
Kenalan Dengan Firebase Android
Kenalan Dengan Firebase AndroidKenalan Dengan Firebase Android
Kenalan Dengan Firebase Android
 
Tutorial Android Membuat Aplikasi senter Flash light
Tutorial Android Membuat Aplikasi senter Flash lightTutorial Android Membuat Aplikasi senter Flash light
Tutorial Android Membuat Aplikasi senter Flash light
 
Android Fast Track CRUD Android PHP MySql
Android Fast Track CRUD Android PHP MySqlAndroid Fast Track CRUD Android PHP MySql
Android Fast Track CRUD Android PHP MySql
 
Belajar Android PHP MySQL Login dengan Volley
Belajar Android PHP MySQL Login dengan VolleyBelajar Android PHP MySQL Login dengan Volley
Belajar Android PHP MySQL Login dengan Volley
 
Adapter & ListView & ExpandalbeListView
Adapter & ListView & ExpandalbeListViewAdapter & ListView & ExpandalbeListView
Adapter & ListView & ExpandalbeListView
 
Belajar Android Membuat Katalog Produk
Belajar Android Membuat Katalog ProdukBelajar Android Membuat Katalog Produk
Belajar Android Membuat Katalog Produk
 
Android development - ListView & Adapter
Android development - ListView & AdapterAndroid development - ListView & Adapter
Android development - ListView & Adapter
 
Belajar Android Studio CRUD Data Mahasiswa
Belajar Android Studio CRUD Data MahasiswaBelajar Android Studio CRUD Data Mahasiswa
Belajar Android Studio CRUD Data Mahasiswa
 
Introduction to Listview in Android
Introduction to Listview in AndroidIntroduction to Listview in Android
Introduction to Listview in Android
 
ListView and Custom ListView on Android Development [Thai]
ListView and Custom ListView on Android Development [Thai]ListView and Custom ListView on Android Development [Thai]
ListView and Custom ListView on Android Development [Thai]
 

Último

WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2
 
tonesoftg
tonesoftgtonesoftg
tonesoftglanshi9
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Bert Jan Schrijver
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationJuha-Pekka Tolvanen
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyviewmasabamasaba
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfonteinmasabamasaba
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...Shane Coughlan
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024VictoriaMetrics
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrainmasabamasaba
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension AidPhilip Schwarz
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park masabamasaba
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...masabamasaba
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benonimasabamasaba
 
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2
 

Último (20)

WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the Situation
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
 
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
 

Android ListView Tutorial

  • 2. Android ListView Tutorial Almost every Android app contains at least one ListView. ListView is one of several ways of displaying a collection of items, and in many cases it is the most appropriate, especially in cases where the items should appear in a specific order, or where text is displayed. In other cases you might want to consider other types of views, such as a GridView. In this tutorial you learn how to properly use a ListView in your Android apps, by following a ListView example for understanding the building blocks, patterns, and best practices. Please note that the icon set for this example was created by Design Deck.
  • 3. Building Blocks Every ListView requires the following elements, although some of the them may be hidden by using default components provided in the Android SDK: ● Item Layout File - a ListView item XML layout file to be used by the list adapter. Sometimes layout files from the Android SDK such as simple_list_item_1 and simple_list_item_2 are used. In this tutorial we will be using a layout that contains an ImageView for the icon, and TextViews for the title and the description. ● Parent View Layout File - the layout file for the activity or fragment. In this tutorial this element is hidden by using the default view of the ListFragment. For information about using a custom layout with a ListActivity or ListFragment read Custom layout in ListActivity and ListFragment. ● Data Container - used for holding the data of a single ListView item. May be as simple as a String or a complex class. In this tutorial we will be using a class that includes an icon, a title and a description. ● List Adapter - used for creating the views of the ListView items. In this tutorial we will be using a custom list adapter that extends the ArrayAdapter class. ● Activity or Fragment - the ListView can be used directly or by using a ListActivity or ListFragment which are
  • 4. Items Layout File The item layout file is used by the list adapter for creating the row view. Below is an example of a ListView row: The row view is described in the res/layout/listview_item.xml file below: 1. <?xml version="1.0" encoding="utf-8"?> 2. <!-- the parent view - provides the gray background --> 3. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 4. android:layout_width="match_parent" 5. android:layout_height="match_parent" 6. android:layout_gravity="center" 7. android:gravity="center_vertical" 8. android:background="@color/frame_background" 9. android:padding="5dp" >
  • 5. Items Layout File 1. 2. <!-- the innner view - provides the white rectangle --> 3. <RelativeLayout android:layout_width="fill_parent" 4. android:layout_height="wrap_content" 5. android:background="@drawable/frame" > 6. 7. <!-- the icon view --> 8. <ImageView android:id="@+id/ivIcon" 9. android:layout_width="wrap_content" 10. android:layout_height="wrap_content" 11. android:padding="5dp" 12. android:contentDescription="@string/icon_content_description" 13. android:scaleType="fitXY" 14. android:layout_alignParentLeft="true" />
  • 6. Items Layout File 1. 2. <!-- the container view for the title and description --> 3. <RelativeLayout android:layout_width="wrap_content" 4. android:layout_height="wrap_content" 5. android:layout_toRightOf="@id/ivIcon" 6. android:layout_centerVertical="true" > 7. 8. <!-- the title view --> 9. <TextView android:id="@+id/tvTitle" 10. android:layout_width="wrap_content" 11. android:layout_height="wrap_content" 12. android:textAppearance="@android:style/TextAppearance.Medium" />
  • 7. Items Layout File 1. <!-- the description view --> 2. <TextView android:id="@+id/tvDescription" 3. android:layout_below="@id/tvTitle" 4. android:layout_width="wrap_content" 5. android:layout_height="wrap_content" 6. android:textAppearance="@android:style/TextAppearance.Small" /> 7. </RelativeLayout> 8. 9. </RelativeLayout> 10. 11. </RelativeLayout>
  • 8. Items Layout File The layout colors for this example are defined in the res/values/colors.xml file below: 1. <?xml version="1.0" encoding="utf-8"?> 2. <resources> 3. <color name="frame">#b4b4b4</color> 4. <color name="frame_solid">#eee</color> 5. <color name="frame_background">#bcbcbc</color> 6. </resources> The rectangular frame in this example is defined in the res/drawable/frame.xml file below: 1. <?xml version="1.0" encoding="utf-8"?> 2. <shape xmlns:android="http://schemas.android.com/apk/res/android" 3. android:shape="rectangle" > 4. 5. <corners android:radius="2dp" /> 6. <stroke android:width="1dp" android:color="@color/frame" /> 7. <solid android:color="@color/frame_solid"/> 8. </shape>
  • 9. Parent View Layout File In this example I used the default view of the ListFragment. For information about using a custom layout with a ListActivity or ListFragment read Custom layout in ListActivity and ListFragment.
  • 10. Data Container The ListViewItem class below is used for holding the data of the ListView item: 1. public class ListViewItem { 2. public final Drawable icon; // the drawable for the ListView item ImageView 3. public final String title; // the text for the ListView item title 4. public final String description; // the text for the ListView item description 5. 6. public ListViewItem(Drawable icon, String title, String description) { 7. this.icon = icon; 8. this.title = title; 9. this.description = description; 10. }
  • 11. List Adapter The list adapter class in this example extends the ArrayAdpter class. It overrides the getView() method of the ArrayAdapter in which the row view is created, and uses the View Holder pattern which prevents using findViewById() repeatedly. Below is the ListViewDemoAdapter class used in this example: 1. public class ListViewDemoAdapter extends ArrayAdapter<ListViewItem> { 2. 3. public ListViewDemoAdapter(Context context, List<ListViewItem> items) { 4. super(context, R.layout.listview_item, items); 5. }
  • 12. List Adapter 1. @Override 2. public View getView(int position, View convertView, ViewGroup parent) { 3. ViewHolder viewHolder; 4. 5. if(convertView == null) { 6. // inflate the GridView item layout 7. LayoutInflater inflater = LayoutInflater.from(getContext()); 8. convertView = inflater.inflate(R.layout.listview_item, parent, false); 9. 10. // initialize the view holder 11. viewHolder = new ViewHolder(); 12. viewHolder.ivIcon = (ImageView) convertView.findViewById(R.id.ivIcon); 13. viewHolder.tvTitle = (TextView) convertView.findViewById(R.id.tvTitle); 14. viewHolder.tvDescription = (TextView) convertView.findViewById(R.id.tvDescription); 15. convertView.setTag(viewHolder); 16. } else { 17. // recycle the already inflated view 18. viewHolder = (ViewHolder) convertView.getTag(); 19. } 20. 21. // update the item view 22. ListViewItem item = getItem(position); 23. viewHolder.ivIcon.setImageDrawable(item.icon); 24. viewHolder.tvTitle.setText(item.title); 25. viewHolder.tvDescription.setText(item.description); 26. 27. return convertView; 28. }
  • 13. List Adapter 1. /** 2. * The view holder design pattern prevents using findViewById() 3. * repeatedly in the getView() method of the adapter. 4. * 5. * @see http://developer.android.com/training/improving-layouts/smooth- scrolling.html#ViewHolder 6. */ 7. private static class ViewHolder { 8. ImageView ivIcon; 9. TextView tvTitle; 10. TextView tvDescription; 11. }
  • 14. Activity or Fragment In this example a ListFragment is used for displaying the ListView. Using a ListActivity or ListFragment instead of a simple Activity or Fragment saves a lot of boilerplate code such as setting the ListView, implementing the OnClickListener interface, and other code encapsulated in convenience methods such as setListAdaper() and getListView(). Below is the ListViewDemoAdapter class used in this example: 1. public class ListViewDemoFragment extends ListFragment { 2. private List<ListViewItem> mItems; // ListView items list 3. 4. @Override 5. public void onCreate(Bundle savedInstanceState) { 6. super.onCreate(savedInstanceState); 7. 8. // initialize the items list 9. mItems = new ArrayList<ListViewItem>(); 10. Resources resources = getResources();
  • 15. Activity or Fragment 1. @Override 2. public void onViewCreated(View view, Bundle savedInstanceState) { 3. super.onViewCreated(view, savedInstanceState); 4. // remove the default dividers from the ListView of the ListFragment 5. getListView().setDivider(null); 6. } 7. 8. @Override 9. public void onListItemClick(ListView l, View v, int position, long id) { 10. // retrieve theListView item 11. ListViewItem item = mItems.get(position); 12. 13. // do something 14. Toast.makeText(getActivity(), item.title, Toast.LENGTH_SHORT).show(); 15. } 16. }