2012-07-24 20 views
7

Utworzono ListView, który używa FastScroll. (Patrz pic) Gdy użytkownik kliknie którykolwiek przycisk poniżej (tzn. Wszystkie utwory, Artyści, Album), za każdym razem następujące zwyczaj ArrayAdater nazywaAndroid: FastScrolling SectionIndexer getSections() jest wywoływana tylko raz.

ArrayAdapter<String> adapter = new ScrollIndexListAdapter(Listing.this, elements); 
//Code for ScrollIndexListAdapter is below 

i tym samym ListView jest aktualizowany.

enter image description here

PROBLEM: Według mojego dochodzenia w Androidzie, metoda getSections() jest wywoływana tylko raz (to tylko wtedy, gdy pierwszy czas ScrollIndexListAdapter nazywa).

Tym razem sekcje są wypełnione & FastScrolling działa idealnie.

Ale kiedy aktualizuję ListView klikając na Artyści/Album, metoda getSections() nie jest wywoływana. Używamy starszych sekcji, a FastScrolling wciąż pokazuje podglądy starych alfabetów.

W jaki sposób można zaktualizować sekcje za każdym razem, gdy lista jest aktualizowana?

Istnieje metoda setSections(), ale nie jestem w stanie znaleźć sposobu jej użycia.

Kod klasy ScrollIndexListAdapter:

public class ScrollIndexListAdapter extends ArrayAdapter implements 
     SectionIndexer { 

    // Variables for SectionIndexer List Fast Scrolling 
    HashMap<String, Integer> alphaIndexer; 
    String[] sections; 
    private static ArrayList<String> list = new ArrayList<String>(); 

    public ScrollIndexListAdapter(Context context, ArrayList<String> list) { 
     super(context, android.R.layout.simple_list_item_1, android.R.id.text1, 
       list); 
     this.list.clear(); 
     this.list.addAll(list); 
     /* 
     * Setting SectionIndexer 
     */ 
     alphaIndexer = new HashMap<String, Integer>(); 
     int size = list.size(); 
     for (int x = 0; x < size; x++) { 
      String s = (String) list.get(x); 
      // Get the first character of the track 
      String ch = s.substring(0, 1); 
      // convert to uppercase otherwise lowercase a -z will be sorted 
      // after upper A-Z 
      ch = ch.toUpperCase(); 
      if (!alphaIndexer.containsKey(ch)) { 
       alphaIndexer.put(ch, x); 
      } 
     } 
     Set<String> sectionLetters = alphaIndexer.keySet(); 
     // create a list from the set to sort 
     ArrayList<String> sectionList = new ArrayList<String>(
       sectionLetters); 
     Collections.sort(sectionList); 
     sections = new String[sectionList.size()]; 
     sectionList.toArray(sections); 
    } 

    /* 
    * Methods for AphhabelIndexer for List Fast Scrolling 
    */ 
    @Override 
    public int getPositionForSection(int section) { 
     String letter = (String) sections[section]; 
     return alphaIndexer.get(letter); 
    } 

    @Override 
    public int getSectionForPosition(int position) { 
     String letter = (String) sections[position]; 
     return alphaIndexer.get(letter); 
    } 

    @Override 
    public Object[] getSections() { 
     return sections; 
    } 
} 
+0

Moje doświadczenie na Androida jest ograniczone, ale uważam, że zmiany w źródle danych powinny być transmitowane przez wywołanie 'notifyDataSetChanged()' w adapterze. –

Odpowiedz

2

Wydaje się, że najnowsza wersja FastScroll nie ma tego problemu. Ale w twoim przypadku jest odwrót. Podczas ustawiania adaptera na ListView, wyłącz i włącz szybkie przewijanie. Zobacz poniższy kod:

  adapter = new ScrollIndexListAdapter(this, data); 
      listView.setFastScrollEnabled(false); 
      listView.setAdapter(adapter); 
      listView.setFastScrollEnabled(true); 
+0

Znajduję ten błąd i dla mnie to działa dobrze :) – JMR

Powiązane problemy