Android: FastScrolling SectionIndexer getSections () is called only once

I created a ListView that uses FastScroll . (see pic.) When the user clicks any of the buttons below (all tracks, artists, album), every time the next custom ArrayAdater is called

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

and updated the same ListView.

enter image description here

PROBLEM . According to my research on Android, the getSections() method is called only once (i.e. only when ScrollIndexListAdapter is called )

This time the sections fill up, and fastScrolling works fine.

But when I update the ListView by clicking on Artistists / Album, the getSections() method is not called. Thus, older sections are used, and FastScrolling still shows previews of old alphabets.

So, how can I update sections every time a ListView is updated?

There is a setSections() method, but I cannot find how to use it.

Code for the ScrollIndexListAdapter class:

 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 AZ 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; } } 
+7
source share
1 answer

The latest version of FastScroll does not seem to have this problem. But in your case there is a twist. When installing the adapter in ListView, disable and enable fast scrolling. See code below:

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

All Articles