简体   繁体   English

将textView作为分隔符添加到ListView

[英]Add textView to a ListView as a divider

I am trying to add a TextView to my list view but when i do it i get a force close. 我试图将TextView添加到我的列表视图,但是当我这样做时,我得到了强制关闭。 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. 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);

        }

As stated before, you should try creating your own custom Adapter . 如前所述,您应该尝试创建自己的自定义Adapter

public class MyAdapter extends BaseAdapter

In this there are a few methods, that you need to override in particular getView() and getViewTypeCount() . 在此方法中,您需要重写一些方法,特别是getView()getViewTypeCount() The latter returns the number of types of ListItems that can be in your List (eg song and letter TextView ). 后者返回List中可能存在的ListItems的类型数(例如,歌曲和字母TextView )。

You should check out this guide on adding seperators to a ListView . 您应该查看有关将分隔符添加到ListView 本指南

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM