Reputation: 2939
In Android, how do you use the AlphabetIndexer with numbers? The code below doesn't seem to work
AlphabetIndexer alphabetIndexer =
new AlphabetIndexer(cursor, COLUMN_INDEX,"0123456789")
Upvotes: 9
Views: 951
Reputation: 1235
AlphabetIndexer seems to be working just fine with numbers. I've created a simple ListFragment that combines list of strings and numbers, sorts them and shows them on the list with functional fast scroll. This also works for a list with only numbers.
public class ListFragment extends android.support.v4.app.ListFragment {
public static final String VALUE_COLUMN = "value_column";
public static final String[] PROJECTION = new String[] {
BaseColumns._ID,
VALUE_COLUMN
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new AlphanumericAdapter(getActivity(),
generateDataCursor(getSortedListOfEntries())));
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
getListView().setFastScrollAlwaysVisible(true);
getListView().setFastScrollEnabled(true);
}
private Cursor generateDataCursor(ArrayList<String> entriesAsArrayList) {
MatrixCursor entriesAsCursor = new MatrixCursor(PROJECTION);
for (String entry : entriesAsArrayList) {
entriesAsCursor.addRow(new String[] { "0", entry });
}
return entriesAsCursor;
}
private ArrayList<String> getSortedListOfEntries() {
ArrayList<String> listEntries = new ArrayList<String>();
for (Locale locale : Locale.getAvailableLocales()) {
int randomIntegerBetweenZeroAndNine = (int) (Math.random() * 10);
listEntries.add(String.valueOf(randomIntegerBetweenZeroAndNine));
String countryName = locale.getDisplayCountry();
if (TextUtils.isEmpty(countryName)) listEntries.add(countryName);
}
Collections.sort(listEntries);
return listEntries;
}
private class AlphanumericAdapter extends SimpleCursorAdapter implements SectionIndexer {
private final AlphabetIndexer mAlphabetIndexer;
public AlphanumericAdapter(Context context, Cursor cursor) {
super(context, android.R.layout.simple_list_item_1, cursor, new String[] { VALUE_COLUMN }, new int[] { android.R.id.text1 });
mAlphabetIndexer = new AlphabetIndexer(cursor, 1, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ");
}
@Override
public Object[] getSections() {
return mAlphabetIndexer.getSections();
}
@Override
public int getPositionForSection(int sectionIndex) {
return mAlphabetIndexer.getPositionForSection(sectionIndex);
}
@Override
public int getSectionForPosition(int position) {
return mAlphabetIndexer.getSectionForPosition(position);
}
}
}
Upvotes: 2
Reputation: 6410
What kind of number range are you talking about? 0-9 only? Otherwise, I'm not sure that would do what you want since alphabetically 00000000 probably proceeds 1111111 and 22222222 or 2222. You should probably implement the section indexer interface yourself, which is a pretty trivial interface: http://developer.android.com/reference/android/widget/SectionIndexer.html and you could return section ranges like "0-99". But if you really only want the numbers 0-9 then you can probably user the AlphabetIndexer with a simple CursorWrapper that returns the string value of the numbers in your Cursor. http://developer.android.com/reference/android/database/CursorWrapper.html
Note: This is assuming that your cursor is returning a numeric/non-string type.
Upvotes: 0