In Android, ListView let you arranges components in a vertical scrollable list.
In this tutorial, we will show you 2
1.1 Android Layout file
File : res/layout/list_fruit.xml
1.2 ListView
1.3 Demo
2.1 Images
Get 4 images for demonstration.
2.2 Android Layout file
File : res/layout/list_mobile.xml
2.3 Custom ArrayAdapter
Create a class extends
2.4 ListView
2.5 Demo
In this tutorial, we will show you 2
ListView
examples :- Normal way to display components in
ListView
. - Custom array adapter to customize the item display in
ListView
.
1. Normal ListView example
In this example, we show you how to display a list of fruit name viaListView
, it should be easy and self-explanatory.1.1 Android Layout file
File : res/layout/list_fruit.xml
<?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="20sp" > </TextView>
package com.mkyong.android; import android.app.ListActivity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; import android.widget.AdapterView.OnItemClickListener; public class ListFruitActivity extends ListActivity { static final String[] FRUITS = new String[] { "Apple", "Avocado", "Banana", "Blueberry", "Coconut", "Durian", "Guava", "Kiwifruit", "Jackfruit", "Mango", "Olive", "Pear", "Sugar-apple" }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // no more this // setContentView(R.layout.list_fruit); setListAdapter(new ArrayAdapter<String>(this, R.layout.list_fruit,FRUITS)); ListView listView = getListView(); listView.setTextFilterEnabled(true); listView.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // When clicked, show a toast with the TextView text Toast.makeText(getApplicationContext(), ((TextView) view).getText(), Toast.LENGTH_SHORT).show(); } }); } }

2. Custom ArrayAdapter example
In this example, we show you how to create 4 items in theListView
, and use a custom “ArrayAdapter
” to display different images base on the “item name” in the list.2.1 Images
Get 4 images for demonstration.

File : res/layout/list_mobile.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="5dp" > <ImageView android:id="@+id/logo" android:layout_width="50px" android:layout_height="50px" android:layout_marginLeft="5px" android:layout_marginRight="20px" android:layout_marginTop="5px" android:src="@drawable/windowsmobile_logo" > </ImageView> <TextView android:id="@+id/label" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@+id/label" android:textSize="30px" > </TextView> </LinearLayout>
Create a class extends
ArrayAdapter
and customize the item display in the getView()
method.package com.mkyong.android.adaptor; import com.mkyong.android.R; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.ImageView; import android.widget.TextView; public class MobileArrayAdapter extends ArrayAdapter<String> { private final Context context; private final String[] values; public MobileArrayAdapter(Context context, String[] values) { super(context, R.layout.list_mobile, values); this.context = context; this.values = values; } @Override public View getView(int position, View convertView, ViewGroup parent) { LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); View rowView = inflater.inflate(R.layout.list_mobile, parent, false); TextView textView = (TextView) rowView.findViewById(R.id.label); ImageView imageView = (ImageView) rowView.findViewById(R.id.logo); textView.setText(values[position]); // Change icon based on name String s = values[position]; System.out.println(s); if (s.equals("WindowsMobile")) { imageView.setImageResource(R.drawable.windowsmobile_logo); } else if (s.equals("iOS")) { imageView.setImageResource(R.drawable.ios_logo); } else if (s.equals("Blackberry")) { imageView.setImageResource(R.drawable.blackberry_logo); } else { imageView.setImageResource(R.drawable.android_logo); } return rowView; } }
ListView
, but use above custom adapter to display the list.package com.mkyong.android; import com.mkyong.android.adaptor.MobileArrayAdapter; import android.app.ListActivity; import android.os.Bundle; import android.widget.ListView; import android.widget.Toast; import android.view.View; public class ListMobileActivity extends ListActivity { static final String[] MOBILE_OS = new String[] { "Android", "iOS", "WindowsMobile", "Blackberry"}; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setListAdapter(new MobileArrayAdapter(this, MOBILE_OS)); } @Override protected void onListItemClick(ListView l, View v, int position, long id) { //get selected items String selectedValue = (String) getListAdapter().getItem(position); Toast.makeText(this, selectedValue, Toast.LENGTH_SHORT).show(); } }

Download Source Code
Download both examples – Android-ListView-Example.zip (21 KB)
thank you very much for the list view example!
ReplyDeletepackers and movers noida sector 12 @ http://www.noidapackers.co.in/
ReplyDeletepackers and movers noida sector 53 @ http://www.noidapackers.co.in/
packers and movers noida sector 51 @ http://www.noidapackers.co.in/
packers and movers noida sector 50 @ http://www.noidapackers.co.in/
packers and movers noida sector 49 @ http://www.noidapackers.co.in/
packers and movers noida sector 41 @ http://www.noidapackers.co.in/
packers and movers noida sector 39 @ http://www.noidapackers.co.in/
packers and movers noida sector 62 @ http://www.noidapackers.co.in/
packers and movers noida sector 61 @ http://www.noidapackers.co.in/
nike air max, ray ban outlet, longchamp handbags, tiffany jewelry, longchamp outlet, coach purses, polo ralph lauren, oakley sunglasses, polo ralph lauren outlet, gucci handbags, longchamp outlet online, michael kors outlet, michael kors outlet online, christian louboutin, tory burch outlet online, nike air max, prada outlet, true religion outlet, red bottom shoes, christian louboutin outlet, coach outlet, nike outlet, coach outlet, nike free, ray ban sunglasses, kate spade outlet online, michael kors outlet online, kate spade outlet, burberry outlet online, jordan shoes, true religion, true religion outlet, chanel handbags, burberry outlet online, christian louboutin shoes, prada handbags, oakley vault, cheap oakley sunglasses, michael kors handbags, michael kors outlet store, coach outlet store online, replica watches, tiffany and co, michael kors outlet online sale
ReplyDeletelongchamp, true religion jeans, new balance pas cher, hollister uk, nike air max, timberland pas cher, nike trainers, nike free, ray ban uk, longchamp, mulberry uk, oakley pas cher, tn pas cher, ray ban pas cher, lacoste pas cher, air max, ralph lauren pas cher, burberry pas cher, michael kors, nike air force, lululemon outlet online, longchamp pas cher, hermes pas cher, scarpe hogan, nike blazer pas cher, north face pas cher, nike air max, louboutin, sac vanessa bruno, guess pas cher, michael kors canada, ralph lauren uk, north face, nike free pas cher, abercrombie and fitch, nike air huarache, hollister, michael kors uk, converse pas cher, vans pas cher, nike roshe, air max pas cher, air jordan, nike roshe run
ReplyDeletevalentino shoes, herve leger, iphone case, louboutin, timberland boots, vans, giuseppe zanotti, bottega veneta, lululemon outlet, baseball bats, beats headphones, vans outlet, nfl jerseys, north face jackets, new balance outlet, wedding dresses, mcm handbags, chi flat iron, asics shoes, ferragamo shoes, reebok shoes, ralph lauren, abercrombie, babyliss, abercrombie and fitch, jimmy choo shoes, p90x workout, hollister, nike air max, soccer shoes, mont blanc pens, celine handbags, north face jackets, oakley, converse, insanity workout, soccer jerseys, mac cosmetics, hollister clothing store, air max, birkin bag, nike roshe, instyler ionic styler, ghd
ReplyDeletegucci, links of london uk, ray ban, supra shoes, pandora uk, coach outlet, louis vuitton canada, louis vuitton, louis vuitton uk, ugg outlet, louis vuitton pas cher, toms outlet, louis vuitton handbags, pandora jewelry, uggs canada, wedding dress, replica watches, thomas sabo uk, ugg, louis vuitton, louis vuitton outlet, louis vuitton outlet, hollister canada, cheap ugg boots, sac louis vuitton, marc jacobs handbags, ugg, swarovski jewelry, ugg australia, pandora charms, juicy couture, cheap ugg boots outlet, louis vuitton outlet online, juicy couture outlet, ugg boots clearance, ugg boots, sac lancel, ugg soldes, doke gabbana outlet, montre femme, swarovski uk, karen millen, converse shoes
ReplyDeleteAndroid Tutorial Basic to Advance: Click Here
ReplyDeleteAndroid User Interface Tutorial: Click Here
jianbin1205
ReplyDeletewellensteyn coats
oklahoma city thunder
soccer jerseys,soccer jerseys wholesale,soccer jerseys cheap,soccer jerseys for sale,cheap soccer jersey,usa soccer jersey,football jerseys
oakley outlet
futbol baratas
ugg boots outlet
tiffany jewellery
the north face
tods shoes,tods shoes sale,tods sale,tods outlet online,tods outlet store,tods factory outlet
golden state warriors
cheap ugg boots
lacoste polo
converse sneakers
belstaff jackets
oakley
true religion sale
michael kors handbags clearance
ray-ban sunglasses
oakley sunglasses
tiffany jewelry
valentino shoes
north face outlet
swarovski outlet
nike outlet
green bay packers
black friday
michael kors outlet store
michael kors uk outlet
fred perry shirts
lebron james shoes,lebron 12,lebron 12 shoes,lebron sneakers,lebron iv,lebron 11,lebron 10
nike air max 2014
timberland outlet
2016-5-6 xiaozhengm
ReplyDeletelouis vuitton handbags
ray ban uk
nike running shoes
toms
burberry outlet
ray ban outlet
cheap oakley sunglasses
tiffany outlet
adidas nmd
tory burch handbags
oakley sunglasses wholesale
michael kors outlet
coach outlet
hollister outlet
hollister
coach outlet
louis vuitton handbags
coach outlet online
true religion jeans
michael kors
nike air huarache
air jordan retro
coach outlet
adidas shoes
coach outlet
nike factory outlet
true religion shorts
true religion jeans
air jordans
jeremy scott shoes
oakley vault
ray ban sunglasses
louis vuitton outlet
jordan retro
supra sneakers
nike trainers
supra shoes
toms shoes
coach outlet store online clearances
gucci outlet
Packers and Movers Pune
ReplyDeletePackers and Movers Gurgaon
Packers and Movers Mumbai
Packers and Movers in Bangalore
ReplyDeletePackers and Movers in Delhi
Packers and Movers in Hyderabad
ReplyDeletehttp://getpackers.com/packers-and-movers-bangalore/
http://getpackers.com/packers-and-movers-bangalore-to-hyderabad/
http://getpackers.com/packers-and-movers-bangalore-to-kerala/
http://getpackers.com/packers-and-movers-bangalore-to-gurgaon/
I have read your blog its very attractive and impressive. I like it your blog.
ReplyDeleteJava Training in Chennai Java Training in Chennai | Core Java Training in Chennai
Online Java Training Java 8 Online Training | Java J2EE Online Training | JavaEE Training Institute in Chennai Java Training Institutes
Java Training Institutes in Chennai Java Training in Chennai
Nice looking sites and great work. Pretty nice information. it has a better understanding. thanks for spending time on it.
ReplyDeleteBest Industrial Training in Noida
Best Industrial Training in Noida
It was excellent and really informative.I also like composing something if my downtime. So I could find out something from your write-up. Thanks.
ReplyDeleteBest BCA Colleges in Noida