2012-07-24 21 views
7

He creado un ListView que está usando FastScroll. (Ver foto) Cuando el usuario hace clic en cualquiera de los botones de abajo (es decir. Todas las canciones, artistas, álbum), cada vez que el siguiente ArrayAdater costumbre se llamaAndroid: FastScrolling SectionIndexer getSections() se llama una sola vez

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

y el mismo ListView se actualiza.

enter image description here

PROBLEMA: Según mi investigación en Android, el método getSections() se llama sólo una vez (es decir, solamente cuando se llama al primera vez ScrollIndexListAdapter).

Esta vez las secciones están ocupadas & fastScrolling funciona perfectamente.

Pero cuando actualizo el ListView haciendo clic en Artistas/Álbum, no se llama al método getSections(). Entonces se usan las secciones más antiguas, y FastScrolling todavía muestra vistas previas de alfabetos antiguos.

Entonces, ¿cómo puedo hacer que las secciones se actualicen cada vez que se actualiza ListView?

Hay un método setSections(), pero no puedo encontrar cómo usarlo.

Código

para la clase 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

Mi experiencia en Android es limitada, pero yo diría que los cambios a la fuente de datos subyacente deben transmitirse llamando 'notifyDataSetChanged()' en el adaptador. –

Respuesta

2

Parece que la última versión FastScroll no tiene ese problema. Pero, en tu caso, hay una vuelta. Al configurar el adaptador en ListView, deshabilite y habilite el desplazamiento rápido. Vea el código a continuación:

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

Encuentro este error y para mí esto funciona bien :) – JMR

Cuestiones relacionadas