Activité
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 TestAct extends Activity {
/** Called when the activity is first created. */
ListView test_listView;
Cursor myCursor;
String[] proj;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_app_test);
TestDummyData cTestDummyData = new TestDummyData(
getApplicationContext());
cTestDummyData.open();
cTestDummyData.insertRandomNames();
myCursor = cTestDummyData.fetchAllData();
test_listView = (ListView) findViewById(R.id.pager_list_test);
test_listView.setFastScrollEnabled(true);
test_listView.setAdapter(
new MyCursorAdapter(getApplicationContext(),
android.R.layout.simple_list_item_1, myCursor,
new String[] { TestDummyData.KEY_NAME },// names
new int[] { android.R.id.text1 })
);
cTestDummyData.close();
}
class MyCursorAdapter extends SimpleCursorAdapter implements SectionIndexer {
AlphabetIndexer alphaIndexer;
// HashMap<String, Integer> alphaIndexer;
// String[] sections;
public MyCursorAdapter(Context context, int layout, Cursor c,
String[] from, int[] to) {
super(context, layout, c, from, to);
alphaIndexer = new AlphabetIndexer(c,
myCursor.getColumnIndex(TestDummyData.KEY_NAME),
" ABCDEFGHIJKLMNOPQRSTUVWXYZ");
// ======optional way to get alphaindexer from data
// alphaIndexer = new HashMap<String, Integer>();
// int size = items.size();
//
// for (int x = 0; x < size; x++) {
// String s = items.get(x);
//
// String ch = s.substring(0, 1);
//
// ch = ch.toUpperCase();
//
// alphaIndexer.put(ch, x);
// }
//
// Set<String> sectionLetters = alphaIndexer.keySet();
//
// ArrayList<String> sectionList = new ArrayList<String>(
// sectionLetters);
//
// Collections.sort(sectionList);
//
// sections = new String[sectionList.size()];
//
// sectionList.toArray(sections);
}
@Override
public int getPositionForSection(int section) {
// TODO Auto-generated method stub
return alphaIndexer.getPositionForSection(section);
}
@Override
public int getSectionForPosition(int position) {
// TODO Auto-generated method stub
return alphaIndexer.getSectionForPosition(position);
}
@Override
public Object[] getSections() {
// TODO Auto-generated method stub
return alphaIndexer.getSections();
}
}
}
Classe Utiliser les données factices pour la liste
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 TestDummyData {
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 TestDummyData(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 = "ANDROIDDEVELOPER";
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);
}
}
la classe ci-dessus est un fichier de données factice pour la tâche courante ... list_app_test.xml
<?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/pager_list_test"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
vous venez de donner test_listView.setFastScrollEnabled (true); réponds à ce que je comprends de ta quête.