2

I am following a great coding example over here: This SO question. It is regarding implementing a SectionIndexer interface to an array adapter.

However, how would you do the same thing if your ArrayAdapter is passing an ArrayList< MyObject > not an ArrayList< String >?

For example, this is where my code is different then his code. He has:

class AlphabeticalAdapter extends ArrayAdapter<String> implements SectionIndexer {
private HashMap<String, Integer> alphaIndexer;
private String[] sections;

public AlphabeticalAdapter(Context c, int resource, List<String> data) {

    alphaIndexer = new HashMap<String, Integer>();
    for (int i = 0; i < data.size(); i++) {
        String s = data.get(i).substring(0, 1).toUpperCase();
        alphaIndexer.put(s, i);
    }

    // other stuff

 }

I am having problems adapting that for loop to my situation. I can't measure the size like he does. Where he has the above, my adapter begins with.

 public class CustomAdapter extends ArrayAdapter<Items> implements
    SectionIndexer {

     public ItemAdapter(Context context, Items[] objects) {

Where he is passing one ArrayList, I have to pass in three, but to make that happen, had to wrap in a custom object class. One of the ArrayLists that I want to sort is one of three fields in the class called "name". It is a string obviously.

I want to scroll through that alphabetically with SectionIndex based on that name field. How do I change the example code from the other question to work in this scenario?

Where he has "data.size()", I need something like "name.size()" - I think?

Community
  • 1
  • 1
TheLettuceMaster
  • 15,594
  • 48
  • 153
  • 259

1 Answers1

2

Where he is passing one ArrayList, I have to pass in three, but to make that happen, had to wrap in a custom object class. One of the ArrayLists that I want to sort is one of three fields in the class called "name".

You don't have three ArrayLists, you have an ArrayList of custom objects that were built from three ArrayLists(so the size is the size of the List that you pass to the adapter). From this point of view the only change in your code is to use the name from that custom object Items to build the sections:

for (int i = 0; i < data.size(); i++) {
    String s = data.get(i).name.substring(0, 1).toUpperCase();
    if (!alphaIndexer.containsKey(s)) {
        alphaIndexer.put(s, i);
    }
}
// ...

There aren't other changes. Also you may need to sort the List of Items that you pass to the adapter using:

Collections.sort(mData);

where your Items class must implement the Comparable<Items> interface:

    class Items implements Comparable<Items> {
        String name;
        // ... rest of the code

        @Override
        public int compareTo(Items another) {   
            // I assume that you want to sort the data after the name field of the Items class
            return name.compareToIgnoreCase(another.name);
        }

    }
user
  • 86,916
  • 18
  • 197
  • 190
  • Hey thanks! I edited my code showing one other part that I should have included. I added my Adapter's constructor. I'm actually passing an Array of objects, not ArrayLists, which I believe changes the for loop again? – TheLettuceMaster Jul 25 '12 at 14:36
  • 1
    @KickingLettuce The only change is the syntax to get the size of the data, instead of `data.size()` you would use `data.length` because you use an array. – user Jul 25 '12 at 15:06
  • Thanks again. I did figure that one out through the help of Eclipse. However, it gave me no recommendations on this: 'data.get(i).name.substring(0, 1)' That was the only error I was getting. I figured it would be data[i].name.substring(0,1) but I think it had a problem on that as well. – TheLettuceMaster Jul 25 '12 at 16:23
  • 1
    @KickingLettuce Yeap, I forgot about that. It should be(just to see) `Items it = data[i]; String name = it.name; String section = name.substring(0, 1).toUpperCase();// ...`. Do you get any errors? – user Jul 25 '12 at 16:33
  • I will try this later this evening and get back to you. Thanks! It does look solid to me. I will mark correct once I get this working, for now, I am +1'ing everything. – TheLettuceMaster Jul 25 '12 at 20:20
  • I had same problem your answer helped. i had to implement in already created custom adapter and this peace of code helped a lot. thanks!! – Reshma Dec 15 '14 at 11:46