Skip to main content

Android List View sample

List View  :  As the name suggests it displays items in a list.  The user gets a list of items (a scrollable one when the number of items are not sufficient in the screen )  and when clicked on any item in the list view displays details about the clicked item.
By default the list items are textViews.
If you observe a list view can be seen in Contacts list , messages , songs list etc in a mobile .
A listView can be implemented in Android in two ways.
  1. Using  ListActivity .
  2. Using  ListView in XML file .
Using listActivity

In this we dont write anything in the xml file except the layout. 
My sample xml file looks like this :
   
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
 android:orientation="vertical">
 

Here i am just setting the layout as a linear layout and giving the properties.

And the Activity File looks like this :
  1.  public class ListExampleActivity extends ListActivity implements
  2. OnItemClickListener {
  3. /** Called when the activity is first created. */

  4. ArrayAdapter aa;
  5. ListView lv;
  6. String[] months = { "January", "February", "March", "April", "May", "June", "July" ,"August"       ,"September", "October",  "November" ,"December"};

  7. @Override
  8. public void onCreate(Bundle savedInstanceState) {
  9. super.onCreate(savedInstanceState);

  10. aa = new ArrayAdapter(this,
  11. android.R.layout.simple_list_item_1, months);
  12. setListAdapter(aa);

  13. lv = getListView();
  14. lv.setOnItemClickListener(this);

  15. }

  16. public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3) {
  17. // TODO Auto-generated method stub
  18. Toast.makeText(this,
  19. "Item is clicked " + ((TextView) arg1).getText() + "  " + arg2,
  20. 600).show();
  21. }
  22. }

Adapters are used in order to bind data with views.
In the similar way i am binding the data (the string array ) with the view (list view) using adapters.
Check the line 1 : we are using a ListActivity. 
       line 13 :  creates a arrayadapter with the data binded to it as the last parameter(months)
       line 17  : getting the default list view .
    line 18  : Setting the click event for the list item. (This will invoke the onItemClick())
    line 23  :   Displays the item that is clicked.



There is no need to change the manifest file for this task.


You can also use XML file to get a list layout.


The output will look something like this



Comments

Popular posts from this blog

Image Slide Show

Hi everyone. In this tutorial i am going to show you all the code which when run will display the images as a slideshow. Below is the attached screen shot :  For this i am considering the images from the Sdcard's images folder where i am having all the images. Next to display the images i am using View Flipper concept. I am adding ImageViews to the viewflipper so that the images can be displayed onto the screen. Now lets start  Design an xml layout as below (say main.xml) <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent" >     <ViewFlipper         android:id="@+id/main_flipper"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_alignParentLeft="true"  

Template literals

Template literals: ES6   introduced many cool things among which one is ‘ Template literals ’   This is a new way of representing strings.    Normally we use either “(double quotes) or ‘(single quote) to represent a string in  JavaScript .   For example:   var name=”literal”;   Or    var name=’literal’;   But from ES6 there is a new way of doing it. We can use `(back tick) symbol which is present left to key ‘1’ in keyboard.    Ie.,   var name = `literal`;   Well when we already have “(double quotes) or ‘(single quote), what extra does `(back tick) do?   Template literals  shines well when we have some concatenation of strings or when creating multi-line strings.   Look at below to understand the power of Template literals.   Let's  take an example where we need to form a string where you are forming a string based on a button clicked. For example - “You clicked on login button”.     Old way:   var 

Localization using .ttf files

In my previous post  here  i gave some information on how to use the concept of localization for the languages that are supported by Android. Now suppose if u want to display a language that is not supported by android.......? Then what might be solution? I will be discussing a small example for doing this now. Suppose if you want to display some text like this " हिंदी " or  " தமிழ் " or  some thing like the text   here  any other language that is  not supported by android we can use .ttf files so that we can print the font we are interested. To achieve this first we need to download few .ttf files. You can google for a file you need, download it and place this into the assets folder. In the picture i am having few other ttf files which i included, But for the time being only the DroidSansRegionalAAd.ttf is discussed by me which i used to display the words of few languages of India as in the following picture. Now we need to refer to the ttf file i