Eric
Eric

Reputation: 83

How to use SectionIndexer on dates

I have been searching on ways to use the SectionIndexer on dates in my listView. I would like to show the "short date" when fast scrolling my list. I saw a post from a couple years ago with a guy saying he had it working and he had a different issue, but sadly he didn't post any code for his method.

I figure I will have to do something to my custom ArrayAdapter but I am not really sure what, anyone have any ideas of where I can look for something like this?

Thanks, -Eric

Upvotes: 3

Views: 1834

Answers (2)

Eric
Eric

Reputation: 83

Ok so here is what I ended up doing to get it to work properly, just in case someone else in the future is looking for something similar.

private class TransactionAdapter extends ArrayAdapter<Transaction> implements SectionIndexer
{
    private ArrayList<Transaction> items;
    private Context context;
    LinkedHashMap<String, Integer> dateIndexer;
    String[] sections;

    public TransactionAdapter(Context context, int textViewResourceId, ArrayList<Transaction> items)
    {
        super(context, textViewResourceId, items);
        this.context = context;
        this.items = items;

        this.dateIndexer = new LinkedHashMap<String, Integer>();
        int size = items.size();
        String prDate = " ";

        for (int x = 0; x < size; x++)
        {
            Transaction tr = items.get(x);
            Calendar date = tr.getDate();
            String month = date.getDisplayName(Calendar.MONTH, Calendar.SHORT, Locale.getDefault());
            String year = String.valueOf(date.get(Calendar.YEAR));
            String shortDate = month + " " + year;

            if( !shortDate.equals(prDate))
            {
                this.dateIndexer.put(shortDate, x);
                prDate = shortDate;
            }
        }

        Set<String> sectionDates = this.dateIndexer.keySet();

        // create a list from the set to sort
        ArrayList<String> sectionList = new ArrayList<String>(sectionDates);
        this.sections = new String[sectionList.size()];
        sectionList.toArray(this.sections);
    }

    public int getPositionForSection(int section) 
    {
        return dateIndexer.get(this.sections[section]);
    }

    public int getSectionForPosition(int position) 
    {
        return 0;
    }

    public Object[] getSections() 
    {
        return this.sections;
    }
}

I found the answer in a combination of two places:

  1. This tutorial helped me with the initial implementation of the SectionIndexter: Android ListView with fastscroll and section index

  2. This post fixed an issue I had with the HashMap changing my ordering of my array, it suggested using a LinkedHashMap instead. is the Java HashMap keySet() iteration order consistent

Upvotes: 3

Artyom Kiriliyk
Artyom Kiriliyk

Reputation: 2513

You can use this library. This is the Simpliest Section Adapter available for Android's ListView. It works with list adapters that you already have. No project specific dependencies. Just include the latest jar or the sources to your Android project.

Upvotes: 0

Related Questions