-1

I'm building a music app and I have an "Album" fragment which displays the song's album cover in RecyclerView. What I want is when I click on any of these items(album cover), I should be taken to another activity (albumsDetails.java) that contains the songs of that particular album. And all these songs should be displayed in RecyclerView.

I know how to use intents and I have tried many things but none of them works. I am new to Android Studio.

Album.java

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

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


            Intent i = new Intent(getContext(), AlbumDetails.class);
            i.putExtra("SongName", song.getSongName());
            startActivity(i);

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

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

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

AlbumsDetails.java

  @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){
    }


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


    };
    albumsDetails_reyclerView.setAdapter(albumsDetailsAdapter);
    albumsDetailsAdapter.notifyDataSetChanged();
  }
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Sebin Paul
  • 169
  • 1
  • 15

5 Answers5

1

1)you need call back from adapter to activity.

  • in which you can pass the model that you want to show in next recycler view.for that simply pass the model through the next recyclerview constructor.

2)in next recycler view adapter you have model simply display the values from that model.

note:-assuming you know how to give call back.

Deep Naik
  • 491
  • 1
  • 3
  • 10
0

Inside Recycler View Adapter, from ViewHolder class your can cll

View.setOnClickListener();

for that particular item inside recyclerview

0

You need to get the album id and pass that album id with intent to start activity. And on that activity, use that album id and query the cursor to get the songs related to that album. Hope that answer the question. Below is the code you can use to get allSongsViaAlbum id.

    public List<SongModel> getAllSongsViaAlbumId(long albumId) throws Exception {
    List<SongModel> songList = null;
    if (mIsPermissionGranted) {
        ContentResolver contentResolver = mContext.getContentResolver();

        Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
        String selection = MediaStore.Audio.Media.ALBUM_ID + "=?";
        String [] whereArgs = {String.valueOf(albumId)};
        String sortOrder = MediaStore.Audio.Media.TITLE + " ASC";
        Cursor cursor = contentResolver.query(uri, null, selection, whereArgs, sortOrder);

        if (cursor != null && cursor.moveToFirst()) {
            songList = new ArrayList<>();
            int totalSongs = cursor.getCount();
            LogUtility.debugLog(MediaUtility.class.getSimpleName(), "Total number of audios " + totalSongs);
            while (cursor.moveToNext()) {
                SongModel currentSong = new SongModel();
                currentSong.setAlbum(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM)));
                currentSong.setAlbumId(cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID)));
                currentSong.setArtist(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST)));
                currentSong.setData(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA)));
                currentSong.setTitle(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE)));
                currentSong.setAlbumArt(getAlbumArtViaAlbumId(currentSong.getAlbumId()));

                songList.add(currentSong);
            }
            cursor.close();
        }
    }
    return songList;
}

On click event use below code

    Intent intent = new Intent(this, yourDesireActivity);
    intent.putExtra("album_id", albumId);
    startActivity(intent);

And when new activity starts call getAllSongsViaAlbumId() method and pass that which you get from intent from previous activity you can use below code to get the id

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

Hope that helps you

Abdul Waheed
  • 4,540
  • 6
  • 35
  • 58
0

Firstly you need to have a look how to declare a RecyclerView adapter. This is a very good tutorial : https://antonioleiva.com/recyclerview/

Moreover you need to set tag to each element of your list, Holder.itemView.setTag(HOLDER POSITION); and finally you can declare a click listener where you can retrieve the tag.

 public void onClick(View v) {

                int tempid = (int) v.getTag();

                Intent intent = new Intent(getContext(), destination.class);

                intent.putExtra("ID", (int) v.getTag());

                startActivity(intent);

On the destination class you can retrieve the id by calling id = getIntent().getExtras().getInt("ID");

0

Passing RecyclerView CardView Clicked Item Data To Activity

Handle Button click inside a row in RecyclerView

try this . Use onClickListener inside the adapter Class.

Tomin B Azhakathu
  • 2,656
  • 1
  • 19
  • 28