Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android alphabetindexer with numbers

Tags:

android

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")
like image 307
meeeee Avatar asked Oct 23 '12 09:10

meeeee


1 Answers

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);
    }
  }
}
like image 141
Bartek Filipowicz Avatar answered Sep 18 '22 06:09

Bartek Filipowicz