将 textView 添加到 ListView 作为分隔线
我正在尝试将 TextView 添加到我的列表视图中,但是当我这样做时,我会强制关闭。我试图在歌曲之前显示歌曲名中的第一个字母。歌曲列表是一个字符串数组列表,这一切都在列表视图中。
Collections.sort(songtitle);
TextView divide = (TextView)findViewById(R.layout.song);
adapter = new ArrayAdapter<String>(this,R.layout.song,songtitle);
int l= 0;
while(l < adapter.getCount()-1 ){
if(songtitle.get(l).charAt(0) == songtitle.get(l+1).charAt(0)){
adapter.add(songtitle.get(l));
}else{
String songname1 = songtitle.get(l);
String newString = songname1.substring(0,1);
divide.append(newString);// This is where i get the force close ... I want to display this textView ////
}
l++;
}
setListAdapter(adapter);
}
I am trying to add a TextView to my list view but when i do it i get a force close. I am trying to display the fist letter in the songname before the song. songlist is a string arraylist and this is all in a list view.
Collections.sort(songtitle);
TextView divide = (TextView)findViewById(R.layout.song);
adapter = new ArrayAdapter<String>(this,R.layout.song,songtitle);
int l= 0;
while(l < adapter.getCount()-1 ){
if(songtitle.get(l).charAt(0) == songtitle.get(l+1).charAt(0)){
adapter.add(songtitle.get(l));
}else{
String songname1 = songtitle.get(l);
String newString = songname1.substring(0,1);
divide.append(newString);// This is where i get the force close ... I want to display this textView ////
}
l++;
}
setListAdapter(adapter);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如前所述,您应该尝试创建自己的自定义适配器。
其中有一些方法需要重写,特别是
getView()
和getViewTypeCount()
。后者返回List
中可以包含的 ListItems 类型的数量(例如歌曲和字母TextView
)。您应该查看本指南,了解向
ListView
添加分隔符。As stated before, you should try creating your own custom
Adapter
.In this there are a few methods, that you need to override in particular
getView()
andgetViewTypeCount()
. The latter returns the number of types of ListItems that can be in yourList
(e.g. song and letterTextView
).You should check out this guide on adding seperators to a
ListView
.您必须使用 CustomAdapter 及其自定义视图来执行此操作。
You have to use a CustomAdapter with its custom view to do this.