Favolas
Favolas

Reputation: 7243

LIstView SectionIndexer giving error ArrayIndexOutOfBoundsException

I've implemented an ListView with SectionIndexer. It is working 99% of the times but I found one situation where it is giving error.

The error is:

java.lang.ArrayIndexOutOfBoundsException: length=1; index=1

And is is happening at this code:

    @Override
    public int getPositionForSection(int section) {
        return indexer.get(sections[section]);
    }

Debugging I've found that the above method, for the input I provide at the end of this text, is being called two times. On the second time, when sectionassumes the value 1 it is giving the error.

My adapter is implemented this way:

public class SearchAdapter extends ArrayAdapter<String[]> implements SectionIndexer {
    private final Context context;
    private ArrayList<String[]> foodData;
    private HashMap<String, Integer> indexer;
    private String[] sections;

    public SearchAdapter(Context context, ArrayList<String[]> allFoodData) {
        super(context, R.layout.rowlist, allFoodData);
        this.context = context;
        this.foodData = allFoodData;


        indexer = new HashMap<String, Integer>();
        int size = foodData.size();
        for (int x = 0; x < size; x++) {
            String[] s = foodData.get(x);
            System.out.println("x = " + x + ": foodata[0]=" + s[0] + ": foodata[1]=" + s[1] + ": foodata[2]=" + s[2] + ": foodata[3]=" + s[3]);

            String ch = s[1].substring(0,1);
            ch = ch.toUpperCase();
            if (!indexer.containsKey(ch))
                indexer.put(ch, x);
        }

        Set<String> sectionLetters = indexer.keySet();
        // create a list from the set to sort
        ArrayList<String> sectionList = new ArrayList<String>(sectionLetters);
        Collections.sort(sectionList);
        sections = new String[sectionList.size()];
        sections = sectionList.toArray(sections);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    \\\\\\\ stuff
    }

    // ##################### LISTVIEW INDEX #####################
    @Override
    public Object[] getSections() {
        return sections;
    }

    @Override
    public int getPositionForSection(int section) {
        return indexer.get(sections[section]);
    }

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

The input is:

x = 0: foodata[0]=81: foodata[1]=Iogurte Açucarado batido gordo com cereais e fruta: foodata[2]=PT INSA: foodata[3]=1
x = 1: foodata[0]=80: foodata[1]=Iogurte Açucarado batido gordo com fruta: foodata[2]=PT INSA: foodata[3]=1
x = 2: foodata[0]=70: foodata[1]=Iogurte Açucarado batido meio gordo: foodata[2]=PT INSA: foodata[3]=1
x = 3: foodata[0]=75: foodata[1]=Iogurte Açucarado batido meio gordo com fruta: foodata[2]=PT INSA: foodata[3]=1
x = 4: foodata[0]=69: foodata[1]=Iogurte Açucarado com edulcorante de síntese, batido magro com cereais: foodata[2]=PT INSA: foodata[3]=1
x = 5: foodata[0]=68: foodata[1]=Iogurte Açucarado com edulcorante de síntese, sólido magro: foodata[2]=PT INSA: foodata[3]=1
x = 6: foodata[0]=71: foodata[1]=Iogurte Açucarado líquido meio gordo: foodata[2]=PT INSA: foodata[3]=1
x = 7: foodata[0]=82: foodata[1]=Iogurte Aromatizado açucarado batido gordo: foodata[2]=PT INSA: foodata[3]=1
x = 8: foodata[0]=72: foodata[1]=Iogurte Aromatizado açucarado batido meio gordo: foodata[2]=PT INSA: foodata[3]=1
x = 9: foodata[0]=77: foodata[1]=Iogurte Aromatizado açucarado líquido magro: foodata[2]=PT INSA: foodata[3]=1
x = 10: foodata[0]=73: foodata[1]=Iogurte Aromatizado açucarado líquido meio gordo: foodata[2]=PT INSA: foodata[3]=1
x = 11: foodata[0]=78: foodata[1]=Iogurte Aromatizado açucarado sólido magro: foodata[2]=PT INSA: foodata[3]=1
x = 12: foodata[0]=74: foodata[1]=Iogurte Aromatizado açucarado sólido meio gordo: foodata[2]=PT INSA: foodata[3]=1
x = 13: foodata[0]=79: foodata[1]=Iogurte Natural sólido magro: foodata[2]=PT INSA: foodata[3]=1
x = 14: foodata[0]=76: foodata[1]=Iogurte Natural sólido meio gordo: foodata[2]=PT INSA: foodata[3]=1

As you can see, the index is only of one letter, in this case "I". I've tried with other inputs that produce only one letter index and this is situation is the only one that gives me error.

Any idea why?

Upvotes: 1

Views: 174

Answers (1)

Uniruddh
Uniruddh

Reputation: 4436

This can solve your problem :

public int getPositionForSection(int section) {

        if (section > sections.length - 1) {
            return 0;
        } else {

            return alphaIndexer.get(sections[section]);
        }
    }

Upvotes: 3

Related Questions