插入的TextView在ListView中

问题描述:

特定的地点我有一个列表视图显示在alphebetical为了歌曲通过这种方法被填充插入的TextView在ListView中

public void updatelist(){ 

    Cursor cursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null,null); 
    int j =0; 

    if(cursor.moveToFirst()){ 
     do{ 

      int ALBUM_ID = cursor.getInt((cursor.getColumnIndex(MediaStore.Audio.AlbumColumns.ALBUM_ID))); 

      int pathcolumn = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA); 

      String path1 = cursor.getString(pathcolumn); 

      String album_url = null; 

      Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart"); 
      Uri uri = ContentUris.withAppendedId(sArtworkUri, ALBUM_ID); 

      album_url = uri.toString(); 
      ContentResolver res = this.getContentResolver(); 
       // Album 
       String album_name = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.AlbumColumns.ALBUM)); 

       String year = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.AudioColumns.YEAR)); 
       // String year = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.AlbumColumns.NUMBER_OF_SONGS)); 


       // artist 
       String artist_name = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.ArtistColumns.ARTIST)); 
       // display name 
       String DisplayName = cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME)); 

       //title 
       String Title = cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.TITLE)); 

       songtitle.add(Title); 
       Collections.sort(songtitle); 
       artistname.add(songtitle.indexOf(Title), artist_name); 
       albumname.add(songtitle.indexOf(Title), album_name); 
       path.add(songtitle.indexOf(Title),path1); 
       albumartwork.add(songtitle.indexOf(Title),album_url); 
       j++; 
      }while(cursor.moveToNext()); 
      } 
       Collections.sort(songtitle); 

       adapter = new ArrayAdapter<String>(this,R.layout.song,songtitle); 

       setListAdapter(adapter); 


     } 

我的问题是要插入分频器每当SONGNAME变化的第一个字母。 我有这样的方法来获取SONGNAME的第一个字母,如果它比以前的不同..

private void alphebetdividers(ArrayList<String> songtitle2) { 
    String newString = null; 
    // TODO Auto-generated method stub 
    ArrayList<Character> letters = new ArrayList<Character>(); 
    int i = 0; 
    int j = 0; 
    while(j < songtitle.size()-1){ 
      if(songtitle2.get(i).charAt(0) == songtitle2.get(i+1).charAt(0)){ 
       Toast.makeText(getApplication(), songtitle2.get(i).charAt(0) + "== " + songtitle2.get(i+1).charAt(0), Toast.LENGTH_LONG).show(); 
      }else{ 
       // Display Char with TextView 
       String songName = songtitle2.get(i); 
       newString = songName.substring(0, 1); 


      } 
     j++; 
     i++; 

} 

如何,我会在适当的地点列表视图中显示这一点。谢谢你,如果你知道答案,我会给你一个很好的评价。

您可能需要一个实现SectionIndexer的适配器,特别是AlphabetIndexer。见thisthis

你真的应该使用一个适配器......有库可以为你做所有这些工作,你不应该在一个巨大的循环中自己做!

这里有一些sample code来帮助你开始......注意它有点过时了,它使用了一些不推荐的方法(如managedQuery等)。如果您想完全正确地使用Honeycomb版本中的接口LoaderManager.LoaderCallbacks<Cursor,但这是一个好的开始。

要显示的观点进行排序,你可以利用在ContentProviderquery方法sortOrder说法。