gpt4 book ai didi

带有数字的android alphabetindexer

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:42:23 25 4
gpt4 key购买 nike

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

关于带有数字的android alphabetindexer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13027840/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com