2011-11-22 72 views
0

我想向我的列表視圖中添加一個TextView,但是當我這樣做時,我會收到一個強制關閉。我試圖在歌曲前顯示歌名中的拳頭字母。歌曲列表是一個字符串arraylist,這是所有列表視圖。將textView作爲分隔符添加到ListView中

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

     } 
+0

分享errorLog。還有songtitle arrayList。 –

回答

1

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

+0

+1,尊敬你先生,但你應該提供更多的信息以及這1行的答案。 –

+0

我不知道如何做到這一點,你可以解釋 –

3

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

public class MyAdapter extends BaseAdapter 

在此有幾個方法,你需要特別getView()getViewTypeCount()覆蓋。後者返回可以在List(例如歌曲和字母TextView)中的ListItems類型的數量。

您應該檢查出this guide關於添加分離器到ListView

+0

+1的確切答案。 –