【问题标题】:Insert TextView at Certain Spots in ListView在 ListView 的特定位置插入 TextView
【发布时间】:2011-11-21 20:31:38
【问题描述】:

我有一个按字母顺序显示歌曲的列表视图,通过这种方法填充

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 的第一个字母发生变化时插入分隔符。 我有这个方法来获取歌曲名的第一个字母,如果它与以前的不同..

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

}

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

【问题讨论】:

    标签: android android-listview textview


    【解决方案1】:

    您可能想要一个实现SectionIndexer,特别是AlphabetIndexer 的适配器。请参阅thisthis

    【讨论】:

      【解决方案2】:

      您确实应该使用适配器...有些库可以为您完成所有这些工作,您不必自己在一个巨大的循环中完成!

      这里有一些 sample code 可以帮助您入门...请注意,它有点过时,因为它使用了一些不推荐使用的方法(例如 managedQuery 等)。如果您想完全 100% 正确,您会希望使用 Honeycomb 版本中引入的 LoaderManager.LoaderCallbacks&lt;Cursor 接口,但这是一个好的开始。

      要对显示的视图进行排序,您可以使用ContentProviderquery 方法中的sortOrder 参数。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-02-08
        • 1970-01-01
        • 2013-09-30
        相关资源
        最近更新 更多