0

When I log out the sectionList there are 20 items; for some reason, all but the last item is being entered into the section Array. Can someone see whats wrong here?

UPDATE: I posted the entire class in question; it's relatively short. It is from an Android app.

public class ItemAdapter extends ArrayAdapter<ItemObject> implements
        SectionIndexer {

    private HashMap<String, Integer> alphaIndexer;
    private String[] sections;
    private LayoutInflater inflater;

    public ItemAdapter(Context context, ItemObject[] objects) {
        super(context, R.layout.item_row_layout, objects);

        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        alphaIndexer = new HashMap<String, Integer>();
        int size = objects.length;

        for (int i = 0; i < size; i++) {

            ItemObject it = objects[i];
            String name = it.name;
            String s = name.substring(0, 1);
            s = s.toUpperCase();

            if (!alphaIndexer.containsKey(s)) {
                alphaIndexer.put(s, i);
            }
        }

        Set<String> sectionLetters = alphaIndexer.keySet();
        ArrayList<String> sectionList = new ArrayList<String>(sectionLetters);
        Collections.sort(sectionList);
        sections = new String[sectionList.size()];

        sections = sectionList.toArray(new String[sectionList.size()]);


    }
2
  • 1
    For better help sooner, post an SSCCE. Commented Aug 28, 2012 at 1:49
  • @KickingLettuce you can use ArrayList.toArray. Also, if you want ordered keys, use a TreeMap. Commented Aug 28, 2012 at 1:54

2 Answers 2

3
    for (int i = 0; i < sectionList.size(); i++) {
        sections[i] = sectionList.get(i);
    }

    for (int i = 0; i < sections.length - 1; i++) {
        sections[i] = sectionList.get(i);
    }

Why are you copying the array twice here? Also, why not use an Iterator? Actually, what you should really use is ArrayList.toArray to convert to String[], e.g.

sections = sectionList.toArray(new String[sectionList.size()]);

Additionally, you can have your keys sorted by using a TreeMap as opposed to a HashMap, merely by doing TreeMap.keySet...

The set's iterator returns the keys in ascending order.

Sign up to request clarification or add additional context in comments.

6 Comments

I apologize, I had the wrong code in that for loop. I have updated up top and added more.
@KickingLettuce why are you looping twice to copy the array?
@KickingLettuce where are you logging to determine it's one short?
Thank you, that did what I needed! I also am using the more efficient 'toArray' as well. Thanks again!
@KickingLettuce what about using TreeMap?
|
1

I think the reason behind that is the condition in the for loop i < sections.length - 1;. You are probably logging sections[i] only in the for loop.

4 Comments

You must remove the nextElement definition to prevent an ArrayIndexOutOfBoundsException.
I have updated code; that part you saw in the last for loop was incorrectly left in.
@veer: Correct changed my answer.
@KickingLettuce: But the last for loop you just added doesn't make sense at all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.