Advertisement
Guest User

Untitled

a guest
Sep 25th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.79 KB | None | 0 0
  1. package ch.egsolutions.tutorial;
  2. import java.util.ArrayList;
  3. import java.util.Collections;
  4. import java.util.HashMap;
  5. import java.util.Iterator;
  6. import java.util.List;
  7. import java.util.Random;
  8. import java.util.Set;
  9.  
  10. import android.app.Activity;
  11. import android.content.Context;
  12. import android.os.Bundle;
  13. import android.util.Log;
  14. import android.widget.ArrayAdapter;
  15. import android.widget.ListView;
  16. import android.widget.SectionIndexer;
  17.  
  18. public class AlphabetIndexer extends Activity {
  19.         ListView myListView;
  20.         ArrayList<String> elements;
  21.  
  22.         @Override
  23.         public void onCreate(Bundle savedInstanceState) {
  24.                 super.onCreate(savedInstanceState);
  25.                 setContentView(R.layout.main);
  26.  
  27.                 // elements
  28.                 String s = "QWERTZUIOPASDFGHJKLYXCVBNM";
  29.                 Random r = new Random();
  30.                 elements = new ArrayList<String>();
  31.                 for (int i = 0; i < 300; i++) {
  32.  
  33.                         elements.add(s.substring(r.nextInt(s.length())));
  34.  
  35.                 }
  36.                 Collections.sort(elements); // Must be sorted!
  37.  
  38.                 // listview
  39.                 myListView = (ListView) findViewById(R.id.myListView);
  40.                 myListView.setFastScrollEnabled(true);
  41.                 MyIndexerAdapter<String> adapter = new MyIndexerAdapter<String>(
  42.                                 getApplicationContext(), android.R.layout.simple_list_item_1,
  43.                                 elements);
  44.                 myListView.setAdapter(adapter);
  45.  
  46.         }
  47.  
  48.         class MyIndexerAdapter<T> extends ArrayAdapter<T> implements SectionIndexer {
  49.  
  50.                 ArrayList<String> myElements;
  51.                 HashMap<String, Integer> alphaIndexer;
  52.  
  53.                 String[] sections;
  54.  
  55.                 public MyIndexerAdapter(Context context, int textViewResourceId,
  56.                                 List<T> objects) {
  57.                         super(context, textViewResourceId, objects);
  58.                         myElements = (ArrayList<String>) objects;
  59.                         // here is the tricky stuff
  60.                         alphaIndexer = new HashMap<String, Integer>();
  61.                         // in this hashmap we will store here the positions for
  62.                         // the sections
  63.  
  64.                         int size = elements.size();
  65.                         for (int i = size - 1; i >= 0; i--) {
  66.                                 String element = elements.get(i);
  67.                                 alphaIndexer.put(element.substring(0, 1), i);
  68.                         //We store the first letter of the word, and its index.
  69.                         //The Hashmap will replace the value for identical keys are putted in
  70.                         }
  71.  
  72.                         // now we have an hashmap containing for each first-letter
  73.                         // sections(key), the index(value) in where this sections begins
  74.  
  75.                         // we have now to build the sections(letters to be displayed)
  76.                         // array .it must contains the keys, and must (I do so...) be
  77.                         // ordered alphabetically
  78.  
  79.                         Set<String> keys = alphaIndexer.keySet(); // set of letters ...sets
  80.                         // cannot be sorted...
  81.  
  82.                         Iterator<String> it = keys.iterator();
  83.                         ArrayList<String> keyList = new ArrayList<String>(); // list can be
  84.                         // sorted
  85.  
  86.                         while (it.hasNext()) {
  87.                                 String key = it.next();
  88.                                 keyList.add(key);
  89.                         }
  90.  
  91.                         Collections.sort(keyList);
  92.  
  93.                         sections = new String[keyList.size()]; // simple conversion to an
  94.                         // array of object
  95.                         keyList.toArray(sections);
  96.  
  97.                         // ooOO00K !
  98.  
  99.                 }
  100.  
  101.                 @Override
  102.                 public int getPositionForSection(int section) {
  103.                         // Log.v("getPositionForSection", ""+section);
  104.                         String letter = sections[section];
  105.  
  106.                         return alphaIndexer.get(letter);
  107.                 }
  108.  
  109.                 @Override
  110.                 public int getSectionForPosition(int position) {
  111.  
  112.                         // you will notice it will be never called (right?)
  113.                         Log.v("getSectionForPosition", "called");
  114.                         return 0;
  115.                 }
  116.  
  117.                 @Override
  118.                 public Object[] getSections() {
  119.  
  120.                         return sections; // to string will be called each object, to display
  121.                         // the letter
  122.                 }
  123.  
  124.         }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement