将字符串添加到适配器
我有一个适配器,我想向其中添加一个字符串,但是当我运行此代码时,它只添加ongstoadd 变量中的最后一个字符串...这是一个自定义适配器,它在必要的地方添加分隔符,但在我想要的数组适配器中名称中具有相同首字母的所有字符串......
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);
}
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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个,让我知道会发生什么。
有关更多信息,请参阅
Android:如何使用SectionIndexer
使用 AlphabetIndexer 进行快速滚动 ListView
在 ListView 中创建简单的字母顺序滚动?< /a>
Try this, And let me know what happen..
For more info look at
Android: how to use SectionIndexer
Using AlphabetIndexer for fastscrolling ListView
Create easy alphabetical scrolling in ListView?
尝试如下更改您的 while 并尝试..
Try changing your while as below and try..