Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: FastScrolling SectionIndexer getSections() is called only once

I've created a ListView which is using FastScroll. (see pic) When the user clicks any of the below Button (viz. All Tracks, Artists, Album), everytime the following custom ArrayAdater is called

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

and the same ListView is updated.

enter image description here

PROBLEM: According to my investigation in Android, the getSections() method is called only once (i.e. only when the first time ScrollIndexListAdapter is called).

This time the sections are populated & the fastScrolling works perfectly.

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

So, how can I make sections get updated everytime when the ListView is updated?

There is a setSections() method, but I'm not able to find how to use it.

Code for 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 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;
    }
}
like image 551
reiley Avatar asked Jul 24 '12 20:07

reiley


1 Answers

It seems that latest FastScroll version doesn't have that problem. But, in your case, there's a turn around. When setting the adapter to the ListView, disable and enable the fast scrolling. See the code below:

            adapter = new ScrollIndexListAdapter(this, data);
            listView.setFastScrollEnabled(false);
            listView.setAdapter(adapter);
            listView.setFastScrollEnabled(true);
like image 94
Italo Borssatto Avatar answered Sep 22 '22 03:09

Italo Borssatto