2012-07-24 19 views
7

Ho creato un ListView che utilizza FastScroll. (Vedi foto) Quando l'utente fa clic qualsiasi sul pulsante in basso (vale a dire. Tutte le canzoni, artisti, album), ogni volta che il seguente ArrayAdater personalizzato è chiamatoAndroid: FastScrolling SectionIndexer getSections() viene chiamato solo una volta

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

e lo stesso ListView viene aggiornato.

enter image description here

PROBLEMA: Secondo la mia ricerca in Android, il metodo getSections() viene chiamato una sola volta (cioè solo quando il prima volta ScrollIndexListAdapter si chiama).

Questa volta le sezioni sono popolate & il fastScrolling funziona perfettamente.

Ma quando aggiorno il ListView facendo clic su Artisti/Album, il metodo getSections() non viene chiamato. Quindi vengono utilizzate le sezioni precedenti e il FastScrolling mostra ancora anteprime di vecchi alfabeti.

Quindi, come è possibile aggiornare le sezioni ogni volta che viene aggiornato ListView?

C'è un metodo setSections(), ma non riesco a trovare come usarlo.

Codice per la classe 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

La mia esperienza su Android è limitata, ma direi che le modifiche all'origine dati sottostante dovrebbero essere trasmesse chiamando 'notifyDataSetChanged()' nell'adattatore. –

risposta

2

Sembra che la versione più recente FastScroll non ha questo problema. Ma, nel tuo caso, c'è una svolta. Quando si imposta l'adattatore su ListView, disabilitare e abilitare lo scorrimento veloce. Vedere il codice di seguito:

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

Trovo questo bug e per me funziona bene :) – JMR

Problemi correlati