Using AlphabetIndexer for fastscrolling ListView 快速滚动查找


[Tut]Using AlphabetIndexer for fastscrolling ListView

Postby qlimax » Wed Jan 13, 2010 8:38 pm

Hi everybody, 

someone asked me how to use the AlphabetIndexer class, provided by the android framework, to build a fastscroll listview which uses A-Z indexing 

the last tutorial I wrote,  http://www.anddev.org/tutalphabetic_fas ... 10123.html
describes how to build an AZ indexed listview using your own arraylist of objects. 

This tutorial explain how build an indexed listview using an ordered cursor, 
yes, because the context in witch you can use the AlphabetIndexer class, is when you have an ordered cursor as a source data for the listview. 

But let's speak the code ! 

Syntax: [  Download ] [  Hide ] [  Select ]  [ Contract ]
Using  xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4.  
  5.    android:orientation="vertical"
  6.  
  7.    android:layout_width="fill_parent"
  8.  
  9.    android:layout_height="fill_parent"
  10.  
  11.    >
  12.  
  13.  
  14.  
  15. <ListView
  16.  
  17. android:id="@+id/myListView"
  18.  
  19. android:layout_width="wrap_content"
  20.  
  21. android:layout_height="wrap_content">
  22.  
  23. </ListView>
  24.  
  25.  
  26.  
  27. </LinearLayout>
Parsed in 0.002 seconds, using  GeSHi 1.0.8.4



the purpose of the class below, is to populate a db from which the Cursor will be extracted 
In most cases you can omit that class, because you already have your ordered cursor... 
Syntax: [  Download ] [  Hide ] [  Select ]  [ Contract ]
Using  java Syntax Highlighting
  1. package  ch.egsolutions.alpha ;
  2.  
  3.  
  4.  
  5. import  java.util.Random ;
  6.  
  7.  
  8.  
  9. import  android.content.ContentValues ;
  10.  
  11. import  android.content.Context ;
  12.  
  13. import  android.database.Cursor ;
  14.  
  15. import  android.database.sqlite.SQLiteDatabase ;
  16.  
  17. import  android.database.sqlite.SQLiteOpenHelper ;
  18.  
  19. import  android.util.Log ;
  20.  
  21.  
  22.  
  23.  
  24.  
  25. public  class DbUtil   {
  26.  
  27.  
  28.  
  29.        
  30.  
  31.  
  32.  
  33.          static  final  String KEY_ID = "_id" ;
  34.  
  35.          static  final  String KEY_NAME = "name" ;
  36.  
  37.  
  38.  
  39.        
  40.  
  41.      private  static  final  String DB_NAME = "tutorial" ;
  42.  
  43.          private  static  final  String TABLE_NAME = "names" ;
  44.  
  45.          private  static  final  int DATABASE_VERSION  =  1 ;
  46.  
  47.  
  48.  
  49.          private  static  final  String DATABASE_CREATE  =
  50.  
  51.                  "create table " +TABLE_NAME + " (" +KEY_ID + " integer primary key autoincrement, "
  52.  
  53.                  + KEY_NAME + " varchar not null);" ;
  54.  
  55.  
  56.  
  57.          private  Context context ;
  58.  
  59.          private DatabaseHelper dbHelper ;
  60.  
  61.          private SQLiteDatabase db ;
  62.  
  63.        
  64.  
  65.        
  66.  
  67.          public DbUtil ( Context context ) {
  68.  
  69.                  this. context =context ;
  70.  
  71.                  this. dbHelper = new DatabaseHelper ( this. context ) ;
  72.  
  73.          }
  74.  
  75.        
  76.  
  77.          private  static  class DatabaseHelper  extends SQLiteOpenHelper
  78.  
  79.      {
  80.  
  81.         DatabaseHelper ( Context context )
  82.  
  83.          {
  84.  
  85.              super (context, DB_NAME,  null, DATABASE_VERSION ) ;
  86.  
  87.          }
  88.  
  89.  
  90.  
  91.         @Override
  92.  
  93.          public  void onCreate (SQLiteDatabase db )
  94.  
  95.          {
  96.  
  97.             db. execSQL (DATABASE_CREATE ) ;
  98.  
  99.          }
  100.  
  101.  
  102.  
  103.         @Override
  104.  
  105.          public  void onUpgrade (SQLiteDatabase db,  int oldVersion,
  106.  
  107.                                int newVersion )
  108.  
  109.          {
  110.  
  111.             Log. v ( "DBUTIL""Upgrading database from version "  + oldVersion
  112.  
  113.                    +  " to "
  114.  
  115.                    + newVersion  +  ", which will destroy all old data" ) ;
  116.  
  117.             db. execSQL ( "DROP TABLE IF EXISTS " +TABLE_NAME ) ;
  118.  
  119.             onCreate (db ) ;
  120.  
  121.          }
  122.  
  123.      }    
  124.  
  125.  
  126.  
  127.          public  void open ( ) {
  128.  
  129.                 db =dbHelper. getWritableDatabase ( ) ;
  130.  
  131.          }
  132.  
  133.          public  void close ( ) {
  134.  
  135.                 dbHelper. close ( ) ;
  136.  
  137.          }
  138.  
  139.        
  140.  
  141.        
  142.  
  143.        
  144.  
  145.          public  void insertRandomNames ( ) {
  146.  
  147.                 db. execSQL ( "DELETE FROM " +TABLE_NAME ) ;
  148.  
  149.                  String s  =  "QWERTZUIOPASDFGHJKLYXCVBNM" ;
  150.  
  151.                  Random r  =  new  Random ( ) ;
  152.  
  153.                
  154.  
  155.                 ContentValues values = new ContentValues ( )  ;
  156.  
  157.                  for ( int i = 0 ;i < 200 ;i ++ ) {
  158.  
  159.                         values. clear ( ) ;
  160.  
  161.                 values. put (KEY_NAME, s. substring (r. nextInt (s. length ( ) ) ) ) ;
  162.  
  163.                 db. insert (TABLE_NAME,  null, values ) ;
  164.  
  165.                  }
  166.  
  167.          }
  168.  
  169.        
  170.  
  171.          public  Cursor fetchAllData ( ) {
  172.  
  173.                  return db. rawQuery ( "SELECT * FROM " +TABLE_NAME + " ORDER BY " +KEY_NAME + " ASC", null ) ;            
  174.  
  175.          }
  176.  
  177.        
  178.  
  179. }
  180.  
  181.  
Parsed in 0.039 seconds, using  GeSHi 1.0.8.4




Now: Activity + MyCursorAdapter 
(comments in the code) 

Syntax: [  Download ] [  Hide ] [  Select ]  [ Contract ]
Using  java Syntax Highlighting
  1. package  ch.egsolutions.alpha ;
  2.  
  3. import  android.app.Activity ;
  4.  
  5. import  android.content.Context ;
  6.  
  7. import  android.database.Cursor ;
  8.  
  9. import  android.os.Bundle ;
  10.  
  11. import  android.widget.AlphabetIndexer ;
  12.  
  13. import  android.widget.ListView ;
  14.  
  15. import  android.widget.SectionIndexer ;
  16.  
  17. import  android.widget.SimpleCursorAdapter ;
  18.  
  19.  
  20.  
  21. public  class AlphabetIndexerActivity  extends Activity  {
  22.  
  23.      /** Called when the activity is first created. */
  24.  
  25.          ListView myListView ;
  26.  
  27.          Cursor myCursor ;
  28.  
  29.          String [ ] proj ;
  30.  
  31.     @Override
  32.  
  33.      public  void onCreate (Bundle savedInstanceState )  {
  34.  
  35.          super. onCreate (savedInstanceState ) ;
  36.  
  37.         setContentView (R. layout. main ) ;
  38.  
  39.  
  40.  
  41.         DbUtil dbUtil = new DbUtil (getApplicationContext ( ) ) ;
  42.  
  43.         dbUtil. open ( ) ;
  44.  
  45.         dbUtil. insertRandomNames ( ) ;
  46.  
  47.         myCursor =dbUtil. fetchAllData ( ) ;  //getting my ordered cursor
  48.  
  49.        
  50.  
  51.         myListView = ( ListView )findViewById (R. id. myListView ) ;
  52.  
  53.         myListView. setFastScrollEnabled ( true ) ;  //must be enabled
  54.  
  55.  
  56.  
  57.         myListView. setAdapter (
  58.  
  59.                        
  60.  
  61.                          new MyCursorAdapter (
  62.  
  63.                         getApplicationContext ( ),
  64.  
  65.                         android. R. layout. simple_list_item_1,
  66.  
  67.                         myCursor,
  68.  
  69.                          new  String [ ] {DbUtil. KEY_NAME }, //from
  70.  
  71.                          new  int [ ] {android. R. id. text1 } )  //to
  72.  
  73.                
  74.  
  75.          ) ;
  76.  
  77.        
  78.  
  79.         dbUtil. close ( ) ;
  80.  
  81.        
  82.  
  83.        
  84.  
  85.      }
  86.  
  87.    
  88.  
  89.    
  90.  
  91.  
  92.  
  93.    
  94.  
  95.      class MyCursorAdapter  extends SimpleCursorAdapter  implements SectionIndexer {
  96.  
  97.  
  98.  
  99.         AlphabetIndexer alphaIndexer ;
  100.  
  101.                  public MyCursorAdapter ( Context context,  int layout,  Cursor c,
  102.  
  103.                                  String [ ] from,  int [ ] to )  {
  104.  
  105.                          super (context, layout, c, from, to ) ;
  106.  
  107.                        
  108.  
  109. alphaIndexer = new AlphabetIndexer (c, myCursor. getColumnIndex (DbUtil. KEY_NAME )" ABCDEFGHIJKLMNOPQRSTUVWXYZ" ) ;
  110.  
  111.  
  112.  
  113. // you have just to instanciate the indexer class like this
  114.  
  115. //cursor,index of the sorted colum,a string representing the alphabeth (pay attention on the blank char at the beginning of the sequence)
  116.  
  117.                        
  118.  
  119.                        
  120.  
  121.                        
  122.  
  123.                  }
  124.  
  125.  
  126.  
  127.                 @Override
  128.  
  129.                  public  int getPositionForSection ( int section )  {                
  130.  
  131.                          return alphaIndexer. getPositionForSection (section ) ;  //use the indexer
  132.  
  133.                  }
  134.  
  135.  
  136.  
  137.                 @Override
  138.  
  139.                  public  int getSectionForPosition ( int position )  {                       
  140.  
  141.                          return alphaIndexer. getSectionForPosition (position ) ;  //use the indexer
  142.  
  143.                  }
  144.  
  145.  
  146.  
  147.                 @Override
  148.  
  149.                  public  Object [ ] getSections ( )  {
  150.  
  151.                          return alphaIndexer. getSections ( ) ;  //use the indexer
  152.  
  153.                  }
  154.  
  155.        
  156.  
  157.      }
  158.  
  159.    
  160.  
  161.    
  162.  
  163.    
  164.  
  165. }
Parsed in 0.040 seconds, using  GeSHi 1.0.8.4


enjoy  :) !
ATTACHMENTS
tut.png
tut.png (127.35 KiB) Viewed 19588 times
¯`·.¸¸.><((((º>¯`·.¸¸. ><((((º>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值