简体   繁体   中英

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

        }

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 (eg song and letter TextView ).

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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