Monday, November 7, 2011

Android Snippet Code: Android ListView with fast scroll and alphabetical section index

Hi,
This small tutorial will show you how to create a ListView with fast scrolling and alphabetical section list that displays the letter as you quickly scroll the list.


We starting with the ListView example of HelloViews resources in the android tutorials and then apply my adapter that implements the alphabetical section indexer.

  1. Start a new project named QuicklySectionListView.
  2. Create an XML file named list_item.xml and save it inside the res/layout/ folder. This file defines the layout for each item that will be placed in the ListView. Insert the following:
     <?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>  
    
  3. Starting from the ListView example I have added an inner class that extends ArrayAdapter and implements SectionIndexer. The code is as follow:
     
    
     package com.example.quicklyscrolllistview;  
       
     import java.util.ArrayList;  
     import java.util.Collections;  
     import java.util.HashMap;  
     import java.util.LinkedList;  
     import java.util.Set;  
       
     import android.app.ListActivity;  
     import android.content.Context;  
     import android.os.Bundle;  
     import android.view.View;  
     import android.widget.AdapterView;  
     import android.widget.AdapterView.OnItemClickListener;  
     import android.widget.ArrayAdapter;  
     import android.widget.ListView;  
     import android.widget.SectionIndexer;  
     import android.widget.TextView;  
     import android.widget.Toast;  
       
     public class QuicklyScrollListViewActivity extends ListActivity {  
          // Called when the activity is first created.  
          @Override  
          public void onCreate(Bundle savedInstanceState) {  
               super.onCreate(savedInstanceState);  
       
               LinkedList<String> mLinked = new LinkedList<String>();  
               for (int i = 0; i < COUNTRIES.length; i++) {  
                    mLinked.add(COUNTRIES[i]);  
               }  
       
               setListAdapter(new MyListAdaptor(this, mLinked));  
       
               ListView lv = getListView();  
               lv.setFastScrollEnabled(true);  
       
               lv.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();  
                    }  
               });  
          }  
       
          static final String[] COUNTRIES = new String[] { "Afghanistan", "Albania",  
                    "Algeria", "American Samoa", "Andorra", "Angola", "Anguilla",  
                    "Antarctica", "Antigua and Barbuda", "Argentina", "Armenia",  
                    "Aruba", "Australia", "Austria", "Azerbaijan", "Bahrain",  
                    "Bangladesh", "Barbados", "Belarus", "Belgium", "Belize", "Benin",  
                    "Bermuda", "Bhutan", "Bolivia", "Bosnia and Herzegovina",  
                    "Botswana", "Bouvet Island", "Brazil",  
                    "British Indian Ocean Territory", "British Virgin Islands",  
                    "Brunei", "Bulgaria", "Burkina Faso", "Burundi", "Cote d'Ivoire",  
                    "Cambodia", "Cameroon", "Canada", "Cape Verde", "Cayman Islands",  
                    "Central African Republic", "Chad", "Chile", "China",  
                    "Christmas Island", "Cocos (Keeling) Islands", "Colombia",  
                    "Comoros", "Congo", "Cook Islands", "Costa Rica", "Croatia",  
                    "Cuba", "Cyprus", "Czech Republic",  
                    "Democratic Republic of the Congo", "Denmark", "Djibouti",  
                    "Dominica", "Dominican Republic", "East Timor", "Ecuador", "Egypt",  
                    "El Salvador", "Equatorial Guinea", "Eritrea", "Estonia",  
                    "Ethiopia", "Faeroe Islands", "Falkland Islands", "Fiji",  
                    "Finland", "Former Yugoslav Republic of Macedonia", "France",  
                    "French Guiana", "French Polynesia", "French Southern Territories",  
                    "Gabon", "Georgia", "Germany", "Ghana", "Gibraltar", "Greece",  
                    "Greenland", "Grenada", "Guadeloupe", "Guam", "Guatemala",  
                    "Guinea", "Guinea-Bissau", "Guyana", "Haiti",  
                    "Heard Island and McDonald Islands", "Honduras", "Hong Kong",  
                    "Hungary", "Iceland", "India", "Indonesia", "Iran", "Iraq",  
                    "Ireland", "Israel", "Italy", "Jamaica", "Japan", "Jordan",  
                    "Kazakhstan", "Kenya", "Kiribati", "Kuwait", "Kyrgyzstan", "Laos",  
                    "Latvia", "Lebanon", "Lesotho", "Liberia", "Libya",  
                    "Liechtenstein", "Lithuania", "Luxembourg", "Macau", "Madagascar",  
                    "Malawi", "Malaysia", "Maldives", "Mali", "Malta",  
                    "Marshall Islands", "Martinique", "Mauritania", "Mauritius",  
                    "Mayotte", "Mexico", "Micronesia", "Moldova", "Monaco", "Mongolia",  
                    "Montserrat", "Morocco", "Mozambique", "Myanmar", "Namibia",  
                    "Nauru", "Nepal", "Netherlands", "Netherlands Antilles",  
                    "New Caledonia", "New Zealand", "Nicaragua", "Niger", "Nigeria",  
                    "Niue", "Norfolk Island", "North Korea", "Northern Marianas",  
                    "Norway", "Oman", "Pakistan", "Palau", "Panama",  
                    "Papua New Guinea", "Paraguay", "Peru", "Philippines",  
                    "Pitcairn Islands", "Poland", "Portugal", "Puerto Rico", "Qatar",  
                    "Reunion", "Romania", "Russia", "Rwanda", "Sqo Tome and Principe",  
                    "Saint Helena", "Saint Kitts and Nevis", "Saint Lucia",  
                    "Saint Pierre and Miquelon", "Saint Vincent and the Grenadines",  
                    "Samoa", "San Marino", "Saudi Arabia", "Senegal", "Seychelles",  
                    "Sierra Leone", "Singapore", "Slovakia", "Slovenia",  
                    "Solomon Islands", "Somalia", "South Africa",  
                    "South Georgia and the South Sandwich Islands", "South Korea",  
                    "Spain", "Sri Lanka", "Sudan", "Suriname",  
                    "Svalbard and Jan Mayen", "Swaziland", "Sweden", "Switzerland",  
                    "Syria", "Taiwan", "Tajikistan", "Tanzania", "Thailand",  
                    "The Bahamas", "The Gambia", "Togo", "Tokelau", "Tonga",  
                    "Trinidad and Tobago", "Tunisia", "Turkey", "Turkmenistan",  
                    "Turks and Caicos Islands", "Tuvalu", "Virgin Islands", "Uganda",  
                    "Ukraine", "United Arab Emirates", "United Kingdom",  
                    "United States", "United States Minor Outlying Islands", "Uruguay",  
                    "Uzbekistan", "Vanuatu", "Vatican City", "Venezuela", "Vietnam",  
                    "Wallis and Futuna", "Western Sahara", "Yemen", "Yugoslavia",  
                    "Zambia", "Zimbabwe" };  
       
          /**  
           * The List row creator  
           */  
          class MyListAdaptor extends ArrayAdapter<String> implements  
                    SectionIndexer {  
       
               HashMap<String, Integer> alphaIndexer;  
               String[] sections;  
       
               public MyListAdaptor(Context context, LinkedList<String> items) {  
                    super(context, R.layout.list_item, items);  
       
                    alphaIndexer = new HashMap<String, Integer>();  
                    int size = items.size();  
       
                    for (int x = 0; x < size; x++) {  
                         String s = items.get(x);  
       
                         // get the first letter of the store  
                         String ch = s.substring(0, 1);  
                         // convert to uppercase otherwise lowercase a -z will be sorted  
                         // after upper A-Z  
                         ch = ch.toUpperCase();  
    
     
    // put only if the key does not exist  
                         if (!alphaIndexer.containsKey(ch))
    
    alphaIndexer.put(ch, x);
    } Set<String> sectionLetters = alphaIndexer.keySet(); // create a list from the set to sort ArrayList<String> sectionList = new ArrayList<String>( sectionLetters); Collections.sort(sectionList); sections = new String[sectionList.size()]; sectionList.toArray(sections); } public int getPositionForSection(int section) { return alphaIndexer.get(sections[section]); } public int getSectionForPosition(int position) { return 0; } public Object[] getSections() { return sections; } } }
MyListAdaptor get a LinkedList of strings and sort it out alphabetically. The indexes have the values of the keys in the LinkedList sorted alphabetically. 
I hope this is helpful.




You can find the source code here.

9 comments :

  1. excellent solution!
    thank you very much!

    ReplyDelete
  2. how to implement if data source in arraylist

    ReplyDelete
    Replies
    1. In the code instead of LinkedList use List, so when you create the "mLinked" variable you can specify your type. In your case becomes:
      List mLinked = new ArrayList();

      ASAP i will update the post! Thanks

      Delete
  3. Thanks for this post! It worked great for me, except for one tweak I had to do: In the loop that fills the alphabetIndexer HashMap I kept track of the current char. HashMap will prevent duplicates, as you correctly stated, but the position for that key will be updated anyway. So I was always seeing the following letter in the alphabet ( e.g.: E while I was at D). Please correct me if I am wrong. Cheers!

    ReplyDelete
    Replies
    1. You're right! The key should be added only if not exist:
      // put only if the key does not exist
      if (!alphaIndexer.containsKey(ch))
      alphaIndexer.put(ch, x);

      Thanks for the advice!

      Delete
  4. I tried using this section indexer within a list given as a fragment. Im using swipelistview of fortysevendeg. The scroll doesnt work smoothly. Sometimes it doesnt work or v need to scroll twice to make it work. My list has about 400 items. Any suggestions to fix this?

    ReplyDelete
  5. amazing solution...Thank you :)

    ReplyDelete
  6. Such a nice post. Thanks for sharing this kind of information. Very useful for all. Keep posting. android training centre in pune

    ReplyDelete