将textView作为分隔符添加到ListView

时间:2011-11-22 07:21:09

标签: android listview textview adapter

我正在尝试将TextView添加到我的列表视图中,但是当我这样做时,我会关闭一个力。我试图在歌曲之前显示歌曲名称中的第一个字母。 songlist是一个字符串arraylist,这都在列表视图中。

 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);

        }

2 个答案:

答案 0 :(得分:3)

如前所述,您应该尝试创建自己的自定义Adapter

public class MyAdapter extends BaseAdapter

在这方面,您需要覆盖一些方法,特别是getView()getViewTypeCount()。后者返回可以在List中的ListItem类型的数量(例如歌曲和字母TextView)。

关于将分隔符添加到ListView

,您应该查看this guide

答案 1 :(得分:1)

您必须使用CustomAdapter及其自定义视图才能执行此操作。