0

I have an adapter and i want to add a string to it but when i run this code it only add the last string in the songstoadd variable ... That is a custom adapter which adds dividers in where neccessary but in the Array Adapter i want to a all of the strings that have the same first letter in their name ....

SeparatedListAdapter adapter = new SeparatedListAdapter(this);
     ArrayList<String> songstoadd = new ArrayList<String>();

              Collections.sort(songtitle);
              int m = 0;
              while( m <songtitle.size()-1){
                  if(songtitle.get(m).substring(0, 1) == songtitle.get(m+1).substring(0, 1)){
                      m++;
                  }else{
                   songstoadd.clear();
                   songstoadd.add(songtitle.get(m));

                adapter.addSection(songtitle.get(m).substring(0, 1), new ArrayAdapter<String>(getApplicationContext(),R.layout.song, songstoadd));

              m++;
                  }



      }
              setListAdapter(adapter);

        }
2
  • i think you need to convert Arraylist to String[] array Commented Nov 22, 2011 at 9:28
  • Look at my edited answer. visit the links.. Commented Nov 22, 2011 at 17:46

2 Answers 2

1

Try this, And let me know what happen..

songstoadd.clear();
 while( m <songtitle.size()-1){
                  if(songtitle.get(m).substring(0, 1).equals( songtitle.get(m+1).substring(0, 1))){
                      m++;
                  }else{

                songstoadd.add(songtitle.get(m));
                adapter.addSection(songtitle.get(m).substring(0, 1), new ArrayAdapter<String>(getApplicationContext(),R.layout.song, songstoadd));

              m++;
                  }

For more info look at

Android: how to use SectionIndexer

Using AlphabetIndexer for fastscrolling ListView

Create easy alphabetical scrolling in ListView?

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

1 Comment

It still adds every single song title to every section
0

Try changing your while as below and try..

while(m <songtitle.size()-1){
     if(songtitle.get(m).substring(0, 1) == songtitle.get(m+1).substring(0, 1)){
         songstoadd.add(songtitle.get(m));
         m++;
     }else{

         songstoadd.add(songtitle.get(m));

         adapter.addSection(songtitle.get(m).substring(0, 1), new ArrayAdapter<String>(getApplicationContext(),R.layout.song, songstoadd));

         songstoadd.clear();

         m++;
    }
}

1 Comment

THis only adds the Section Header and not anything from the songtitle array

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.