如何通过AlbumID获取专辑的歌曲列表?

时间:2017-10-13 14:42:24

标签: android android-intent android-recyclerview android-music-player

我正在制作一个有片段的音乐播放器。它有5个片段 - 歌曲,专辑,艺术家,播放列表,流派。 Song.java片段显示来自recyclerVIew的sd卡中的所有歌曲。并且Albums.java显示SD卡中存在的歌曲的专辑封面(在recyclerView中)。我想要的是当我点击其中一个专辑封面时,它应该带我到另一个活动(AlbumsDetails.java),该活动显示该特定专辑中包含的recyclerView中的所有歌曲(就像在音乐播放器中一样)。我将Album ID作为意图从Albums.java传递给AlbumsDetails.java。我尝试了很多方法但没有成功显示点击专辑的歌曲。

Albums.java

import UIKit
import XCPlayground

let path = UIBezierPath()
path.move(to: CGPoint(x:10,y:10))
path.addLine(to: CGPoint(x:290,y:10))
path.lineWidth = 2

let dashPattern : [CGFloat] = [16, 16]
path.setLineDash(dashPattern, count: 2, phase: 0)
path.lineCapStyle = CGLineCap.round
path.lineCapStyle = .butt

let renderer = UIGraphicsImageRenderer(size: CGSize(width: 300, height: 20))
let image = renderer.image { context in
    path.stroke()
}
// image now contains an image of the path

AlbumsDetails.java(这是应该显示所点击相册的歌曲的类):

public class Albums  extends Fragment  {


private static final String TAG = "Albums";


RecyclerView recyclerViewAlbum;
AlbumsAdapter albumsAdapter;
private ArrayList<SongInfoModel> SongList1 = new ArrayList<SongInfoModel>();
long albumId;


@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.albums_activity, container, false);

    recyclerViewAlbum = view.findViewById(R.id.albums_reyclerView);
    recyclerViewAlbum.setHasFixedSize(true);

    GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(),2);
    recyclerViewAlbum.setLayoutManager(gridLayoutManager);


    Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    String selection = MediaStore.Audio.Media.IS_MUSIC + "!=0";
    Cursor cursor = getActivity().getContentResolver().query(uri, null, selection, null, null);
    if (cursor != null) {
        if (cursor.moveToFirst()) {
            do {
                String name = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
                String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
                long duration = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION));
                String album = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM));
                String data = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
                long albumId = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
                Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
                Uri albumArtUri = ContentUris.withAppendedId(sArtworkUri, albumId);



                SongInfoModel s = new SongInfoModel(name, artist, null, album, null, duration, data,albumArtUri, null);
                SongList1.add(s);

            } while (cursor.moveToNext());
        }


        cursor.close();

          Collections.sort(SongList1, new Comparator<SongInfoModel>() {
            @Override
            public int compare(SongInfoModel lhs, SongInfoModel rhs) {
                return lhs.getAlbum().compareTo(rhs.getAlbum());
            }
        });



    }

    albumsAdapter = new AlbumsAdapter( SongList1,getContext(), new AlbumsAdapter.RecyclerItemClickListener()  {
        @Override
        public void onClickListener(SongInfoModel song, int position) {


            Intent intent = new Intent(getContext(), AlbumDetails.class);
            intent.putExtra("album_id", albumId);
            startActivity(intent);




            Activity activity = getActivity();
            if (activity instanceof MainActivity) {


            }


        }




    });


    recyclerViewAlbum.setAdapter(albumsAdapter);
    albumsAdapter.notifyDataSetChanged();
    return view;

}

AlbumsDetailsAdapter.java(AlbumsDetails.java的适配器类):

public class AlbumDetails extends Activity {


RecyclerView albumsDetails_reyclerView;
AlbumsDetailsAdapter albumsDetailsAdapter;
private ArrayList<SongInfoModel> SongList2 = new ArrayList<SongInfoModel>();
long albumId;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.album_details);

    albumsDetails_reyclerView = findViewById(R.id.albumsDetails_reyclerView);

    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getApplicationContext());
    albumsDetails_reyclerView.setLayoutManager(linearLayoutManager);

    Bundle extras = getIntent().getExtras();
    if(extras != null){

         albumId = getIntent().getExtras().getLong("album_id");

    }


    albumsDetailsAdapter = new AlbumsDetailsAdapter(getApplicationContext(), SongList2, new AlbumsDetailsAdapter.RecyclerItemClickListenerAlbumsDetails() {
        @Override
        public void onClickListener(SongInfoModel songInfoModelAlbumDetails, int positionAlbumDetails) {





        }
    }){


    };





    albumsDetails_reyclerView.setAdapter(albumsDetailsAdapter);
    albumsDetailsAdapter.notifyDataSetChanged();

  }
}

SongInfoModel.java(Model class):

public class AlbumsDetailsAdapter extends RecyclerView.Adapter<AlbumsDetailsAdapter.AlbumsDetailsHolder> {


Context context;
ArrayList<SongInfoModel> SongListAlbumDetails = new ArrayList<>();
private RecyclerItemClickListenerAlbumsDetails listenerAlbumDetails;

public AlbumsDetailsAdapter(Context context, ArrayList<SongInfoModel> songListAlbumDetails, RecyclerItemClickListenerAlbumsDetails listenerAlbumDetails) {
    this.context = context;
    SongListAlbumDetails = songListAlbumDetails;
    this.listenerAlbumDetails = listenerAlbumDetails;
}

@Override
public AlbumsDetailsAdapter.AlbumsDetailsHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(context).inflate(R.layout.row_albumdetails, parent, false);
    return new AlbumsDetailsHolder(view);
}

@Override
public void onBindViewHolder(AlbumsDetailsAdapter.AlbumsDetailsHolder holder, int position) {

    final SongInfoModel songInfoModelAlbumDetails = SongListAlbumDetails.get(position);

    holder.AlbumSongName.setText(songInfoModelAlbumDetails.getSongName());

    holder.bindAlbumsDetails(songInfoModelAlbumDetails, listenerAlbumDetails);

}



@Override
public int getItemCount() {
    return SongListAlbumDetails.size();
}

public class AlbumsDetailsHolder extends RecyclerView.ViewHolder {

    TextView AlbumSongName;

    public AlbumsDetailsHolder(View itemView) {
        super(itemView);

        AlbumSongName = itemView.findViewById(R.id.AlbumSongName);


    }

    public void bindAlbumsDetails(final SongInfoModel songInfoModelAlbumDetails, final RecyclerItemClickListenerAlbumsDetails listenerAlbumDetails) {

        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                listenerAlbumDetails.onClickListener(songInfoModelAlbumDetails, getLayoutPosition());
            }
        });

    }
}



public interface RecyclerItemClickListenerAlbumsDetails{

    void onClickListener(SongInfoModel songInfoModelAlbumDetails, int positionAlbumDetails);


}

0 个答案:

没有答案