Created
January 25, 2014 17:07
-
-
Save bartoszfilipowicz/8619640 to your computer and use it in GitHub Desktop.
Alphabet and numbers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.bartoszfilipowicz.alphanumeric; | |
import android.content.Context; | |
import android.database.Cursor; | |
import android.database.MatrixCursor; | |
import android.os.Bundle; | |
import android.provider.BaseColumns; | |
import android.text.TextUtils; | |
import android.view.View; | |
import android.widget.AlphabetIndexer; | |
import android.widget.SectionIndexer; | |
import android.widget.SimpleCursorAdapter; | |
import java.util.ArrayList; | |
import java.util.Collections; | |
import java.util.Locale; | |
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()) { | |
listEntries.add(String.valueOf(((int) (Math.random() * 10)))); | |
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); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment