创建列表视图,按字母顺序对歌曲进行排序

时间:2014-08-01 16:07:25

标签: android listview sorting albumart

您好,我真的很新的Android和即时开发音乐播放器,到目前为止,我设法填充列表视图,所有我需要按字母顺序排序列表,我想在ImageView中显示歌曲的专辑艺术请帮助。

这是代码

public class MainActivity extends Activity {
ListView musiclist;
Cursor musiccursor;
int music_column_index;
int count;
MediaPlayer mMediaPlayer;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      init_phone_music_grid();
}

private void init_phone_music_grid() {

      String[] proj = { MediaStore.Audio.Media._ID,
  MediaStore.Audio.Media.DATA,
  MediaStore.Audio.Media.TITLE,
  MediaStore.Audio.Albums.ALBUM_ID,
  MediaStore.Audio.Albums._ID,
  MediaStore.Audio.Albums.ALBUM_ART,
  MediaStore.Video.Media.SIZE };
      musiccursor = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,proj, null, null, null);
      count = musiccursor.getCount();
      musiclist = (ListView) findViewById(R.id.listView);
      musiclist.setAdapter(new MusicAdapter(getApplicationContext()));



      musiclist.setOnItemClickListener(musicgridlistener);
      mMediaPlayer = new MediaPlayer();
}

    private OnItemClickListener musicgridlistener = new OnItemClickListener() {
      public void onItemClick(AdapterView parent, View v, int position,long id) {

            music_column_index = musiccursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
            musiccursor.moveToPosition(position);
            String filename = musiccursor.getString(music_column_index);

            try {
                  if (mMediaPlayer.isPlaying()) {
                        mMediaPlayer.reset();
                  }
                  mMediaPlayer.setDataSource(filename);
                  mMediaPlayer.prepare();
                  mMediaPlayer.start();
            } catch (Exception e) {

            }
      }
};

     public class MusicAdapter extends BaseAdapter {
      private Context mContext;

      public MusicAdapter(Context c) {
            mContext = c;
      }

      public int getCount() {
            return count;
      }

      public Object getItem(int position) {
            return position;
      }

      public long getItemId(int position) {
            return position;
      }

      @Override
      public View getView(int position, View convertView, ViewGroup parent) {

          String id = null;
          String id1 = null;
          LayoutInflater inflater = (LayoutInflater) mContext
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.rowlayout, parent, false);
        music_column_index = musiccursor
        .getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE);
          musiccursor.moveToPosition(position);
                    id = musiccursor.getString(music_column_index);
                              music_column_index = musiccursor
                                   .getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE);
                                                   musiccursor.moveToPosition(position);
                                                       id1 = musiccursor.getString(music_column_index);                         
        TextView Title = (TextView) rowView.findViewById(R.id.titlelist);
        TextView Artist = (TextView) rowView.findViewById(R.id.artistlist);
        ImageView imageView = (ImageView) rowView.findViewById(R.id.iconlist);
        // What should i do to set the album art to this imageview ????
        Title.setText(id);
        Artist.setText(id1);
        // change the icon for Windows and iPhone


        return rowView;
      }
}
}

1 个答案:

答案 0 :(得分:3)

要获取歌曲的排序列表,请将managedQuery的最后一个参数从null更改为属性,您希望将结果排序在

musiccursor = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,proj, null, null,COLUMN_NAME); 

即。如果你想对歌曲标题上的列表进行排序,请执行类似

的操作
musiccursor = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,proj, null, null,MediaStore.Audio.Media.TITLE);

查看this库。它有助于按字母顺序划分列表,类似于IOS。