将 textView 添加到 ListView 作为分隔线

发布于 2024-12-17 12:04:06 字数 916 浏览 2 评论 0原文

我正在尝试将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

静水深流 2024-12-24 12:04:06

如前所述,您应该尝试创建自己的自定义适配器。

public class MyAdapter extends BaseAdapter

其中有一些方法需要重写,特别是 getView()getViewTypeCount()。后者返回 List 中可以包含的 ListItems 类型的数量(例如歌曲和字母 TextView)。

您应该查看本指南,了解向 ListView 添加分隔符。

As stated before, you should try creating your own custom Adapter.

public class MyAdapter extends BaseAdapter

In this there are a few methods, that you need to override in particular getView() and getViewTypeCount(). The latter returns the number of types of ListItems that can be in your List (e.g. song and letter TextView).

You should check out this guide on adding seperators to a ListView.

挽梦忆笙歌 2024-12-24 12:04:06

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

You have to use a CustomAdapter with its custom view to do this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文