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
-
<?xml version="1.0" encoding="utf-8"?>
-
-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-
-
android:orientation="vertical"
-
-
android:layout_width="fill_parent"
-
-
android:layout_height="fill_parent"
-
-
>
-
-
-
-
<ListView
-
-
android:id="@+id/myListView"
-
-
android:layout_width="wrap_content"
-
-
android:layout_height="wrap_content">
-
-
</ListView>
-
-
-
-
</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
-
package ch.egsolutions.alpha ;
-
-
-
-
import java.util.Random ;
-
-
-
-
import android.content.ContentValues ;
-
-
import android.content.Context ;
-
-
import android.database.Cursor ;
-
-
import android.database.sqlite.SQLiteDatabase ;
-
-
import android.database.sqlite.SQLiteOpenHelper ;
-
-
import android.util.Log ;
-
-
-
-
-
-
public class DbUtil {
-
-
-
-
-
-
-
-
static final String KEY_ID = "_id" ;
-
-
static final String KEY_NAME = "name" ;
-
-
-
-
-
-
private static final String DB_NAME = "tutorial" ;
-
-
private static final String TABLE_NAME = "names" ;
-
-
private static final int DATABASE_VERSION = 1 ;
-
-
-
-
private static final String DATABASE_CREATE =
-
-
"create table " +TABLE_NAME + " (" +KEY_ID + " integer primary key autoincrement, "
-
-
+ KEY_NAME + " varchar not null);" ;
-
-
-
-
private Context context ;
-
-
private DatabaseHelper dbHelper ;
-
-
private SQLiteDatabase db ;
-
-
-
-
-
-
public DbUtil ( Context context ) {
-
-
this. context =context ;
-
-
this. dbHelper = new DatabaseHelper ( this. context ) ;
-
-
}
-
-
-
-
private static class DatabaseHelper extends SQLiteOpenHelper
-
-
{
-
-
DatabaseHelper ( Context context )
-
-
{
-
-
super (context, DB_NAME, null, DATABASE_VERSION ) ;
-
-
}
-
-
-
-
@Override
-
-
public void onCreate (SQLiteDatabase db )
-
-
{
-
-
db. execSQL (DATABASE_CREATE ) ;
-
-
}
-
-
-
-
@Override
-
-
public void onUpgrade (SQLiteDatabase db, int oldVersion,
-
-
int newVersion )
-
-
{
-
-
Log. v ( "DBUTIL", "Upgrading database from version " + oldVersion
-
-
+ " to "
-
-
+ newVersion + ", which will destroy all old data" ) ;
-
-
db. execSQL ( "DROP TABLE IF EXISTS " +TABLE_NAME ) ;
-
-
onCreate (db ) ;
-
-
}
-
-
}
-
-
-
-
public void open ( ) {
-
-
db =dbHelper. getWritableDatabase ( ) ;
-
-
}
-
-
public void close ( ) {
-
-
dbHelper. close ( ) ;
-
-
}
-
-
-
-
-
-
-
-
public void insertRandomNames ( ) {
-
-
db. execSQL ( "DELETE FROM " +TABLE_NAME ) ;
-
-
String s = "QWERTZUIOPASDFGHJKLYXCVBNM" ;
-
-
Random r = new Random ( ) ;
-
-
-
-
ContentValues values = new ContentValues ( ) ;
-
-
for ( int i = 0 ;i < 200 ;i ++ ) {
-
-
values. clear ( ) ;
-
-
values. put (KEY_NAME, s. substring (r. nextInt (s. length ( ) ) ) ) ;
-
-
db. insert (TABLE_NAME, null, values ) ;
-
-
}
-
-
}
-
-
-
-
public Cursor fetchAllData ( ) {
-
-
return db. rawQuery ( "SELECT * FROM " +TABLE_NAME + " ORDER BY " +KEY_NAME + " ASC", null ) ;
-
-
}
-
-
-
-
}
-
-
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
-
package ch.egsolutions.alpha ;
-
-
import android.app.Activity ;
-
-
import android.content.Context ;
-
-
import android.database.Cursor ;
-
-
import android.os.Bundle ;
-
-
import android.widget.AlphabetIndexer ;
-
-
import android.widget.ListView ;
-
-
import android.widget.SectionIndexer ;
-
-
import android.widget.SimpleCursorAdapter ;
-
-
-
-
public class AlphabetIndexerActivity extends Activity {
-
-
/** Called when the activity is first created. */
-
-
ListView myListView ;
-
-
Cursor myCursor ;
-
-
String [ ] proj ;
-
-
@Override
-
-
public void onCreate (Bundle savedInstanceState ) {
-
-
super. onCreate (savedInstanceState ) ;
-
-
setContentView (R. layout. main ) ;
-
-
-
-
DbUtil dbUtil = new DbUtil (getApplicationContext ( ) ) ;
-
-
dbUtil. open ( ) ;
-
-
dbUtil. insertRandomNames ( ) ;
-
-
myCursor =dbUtil. fetchAllData ( ) ; //getting my ordered cursor
-
-
-
-
myListView = ( ListView )findViewById (R. id. myListView ) ;
-
-
myListView. setFastScrollEnabled ( true ) ; //must be enabled
-
-
-
-
myListView. setAdapter (
-
-
-
-
new MyCursorAdapter (
-
-
getApplicationContext ( ),
-
-
android. R. layout. simple_list_item_1,
-
-
myCursor,
-
-
new String [ ] {DbUtil. KEY_NAME }, //from
-
-
new int [ ] {android. R. id. text1 } ) //to
-
-
-
-
) ;
-
-
-
-
dbUtil. close ( ) ;
-
-
-
-
-
-
}
-
-
-
-
-
-
-
-
-
-
class MyCursorAdapter extends SimpleCursorAdapter implements SectionIndexer {
-
-
-
-
AlphabetIndexer alphaIndexer ;
-
-
public MyCursorAdapter ( Context context, int layout, Cursor c,
-
-
String [ ] from, int [ ] to ) {
-
-
super (context, layout, c, from, to ) ;
-
-
-
-
alphaIndexer = new AlphabetIndexer (c, myCursor. getColumnIndex (DbUtil. KEY_NAME ), " ABCDEFGHIJKLMNOPQRSTUVWXYZ" ) ;
-
-
-
-
// you have just to instanciate the indexer class like this
-
-
//cursor,index of the sorted colum,a string representing the alphabeth (pay attention on the blank char at the beginning of the sequence)
-
-
-
-
-
-
-
-
}
-
-
-
-
@Override
-
-
public int getPositionForSection ( int section ) {
-
-
return alphaIndexer. getPositionForSection (section ) ; //use the indexer
-
-
}
-
-
-
-
@Override
-
-
public int getSectionForPosition ( int position ) {
-
-
return alphaIndexer. getSectionForPosition (position ) ; //use the indexer
-
-
}
-
-
-
-
@Override
-
-
public Object [ ] getSections ( ) {
-
-
return alphaIndexer. getSections ( ) ; //use the indexer
-
-
}
-
-
-
-
}
-
-
-
-
-
-
-
-
}
Parsed in 0.040 seconds, using GeSHi 1.0.8.4
enjoy !