簡體   English   中英

將textView作為分隔符添加到ListView

[英]Add textView to a ListView as a divider

我試圖將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);

        }

如前所述,您應該嘗試創建自己的自定義Adapter

public class MyAdapter extends BaseAdapter

在此方法中,您需要重寫一些方法,特別是getView()getViewTypeCount() 后者返回List中可能存在的ListItems的類型數(例如,歌曲和字母TextView )。

您應該查看有關將分隔符添加到ListView 本指南

您必須使用CustomAdapter及其自定義視圖來執行此操作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM