android alphabetindexer与数字

在Android中,你如何使用AlphabetIndexer和数字? 下面的代码似乎不工作

AlphabetIndexer alphabetIndexer = new AlphabetIndexer(cursor, COLUMN_INDEX,"0123456789") 

Solutions Collecting From Web of "android alphabetindexer与数字"

AlphabetIndexer似乎与数字工作得很好。 我创build了一个简单的ListFragment,它将string和数字组合在一起,对它们进行sorting,并使用function快速滚动将它们显示在列表中。 这也适用于只有数字的列表。

 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); } } } 

你在说什么样的数字范围? 只有0-9? 否则,我不能确定这会做你想要的,因为按字母顺序00000000可能会收益1111111和22222222或2222.你应该自己实现部分索引器接口,这是一个非常平凡的接口: http : //developer.android.com /reference/android/widget/SectionIndexer.html ,你可以返回部分范围,如“0-99”。 但是,如果你真的只想要数字0-9,那么你可以用一个简单的CursorWrapper来使用AlphabetIndexer,它返回光标中数字的string值。 http://developer.android.com/reference/android/database/CursorWrapper.html

注意:这假定你的游标正在返回一个数字/非stringtypes。