Android では、AlphabetIndexer を数字でどのように使用しますか? 以下のコードは動作しないようです
AlphabetIndexer alphabetIndexer =
new AlphabetIndexer(cursor, COLUMN_INDEX,"0123456789")
Android では、AlphabetIndexer を数字でどのように使用しますか? 以下のコードは動作しないようです
AlphabetIndexer alphabetIndexer =
new AlphabetIndexer(cursor, COLUMN_INDEX,"0123456789")
AlphabetIndexer は、数値に対しては問題なく機能しているようです。文字列と数値のリストを組み合わせて並べ替え、機能的な高速スクロールでリストに表示する単純な ListFragment を作成しました。これは、数字のみのリストでも機能します。
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 の数字だけが必要な場合は、Cursor 内の数字の文字列値を返す単純な CursorWrapper を使用して AlphabetIndexer を使用できます。http://developer.android.com/reference/android/database/CursorWrapper.html
注: これは、カーソルが数値/非文字列型を返すことを前提としています。