-1

음악 앱을 만들고 있는데 앨범 표지를 표시하는 "앨범"조각이 있습니다 (RecyclerView). 내가 원하는 것은 이러한 항목 (앨범 표지) 중 하나를 클릭하면 특정 앨범의 노래가 포함 된 다른 활동 (albumsDetails.java)으로 이동해야합니다. 그리고이 노래들은 모두 RecyclerView에 표시되어야합니다. 의도를 사용하는 방법을 알고 많은 일을 시도했지만 그 중 아무 것도 작동하지 않습니다.recyclerView의 항목을 클릭하고 클릭 한 항목의 세부 정보가 포함 된 다른 recyclerView로 이동하는 방법?

안드로이드 스튜디오에 대한 새로운 질문이므로 제발 저의 질문을 잊지 마십시오.

당신이

View.setOnClickListener()를 CLL 수 ViewHolder 클래스에서 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(); 
    } 
} 
+0

어댑터 내부의보기 홀더 클래스를 클릭하십시오. 그것은 어댑터에서 클릭을 취하는 적절한 방법입니다. –

답변

0

내부 휴지통보기 어댑터; recyclerview

당신은 앨범 ID를 얻고 활동을 시작하는 의도로 그 앨범 ID를 전달해야
0

내부의 특정 항목에 대한

. 그 활동에서 그 앨범 ID를 사용하고 커서를 쿼리하여 해당 앨범과 관련된 노래를 가져옵니다. 그 질문에 대답하길 바래. 아래는 allSongsViaAlbum ID를 얻기 위해 사용할 수있는 코드입니다. 새로운 활동의 시작은 getAllSongsViaAlbumId() 메서드를 호출하고 이전 작업 의도에서 얻는 것을 통과 할 때 ID를 얻기 위해 코드 아래 사용할 수 있습니다

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

그리고 코드 아래 클릭 이벤트 사용에

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

당신이

+0

코드를 작성해 주시겠습니까? 괜찮 으면 –

+0

@SebinPaul 코드를 추가했습니다 –

+0

고마워요! 나는 그것을 들여다 볼 것이고 내가 어떤 문제에 직면하면 당신이 알 수있게 할 것이다! –

1

1) 나 전화를 필요로하는 데 도움이

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

희망 어댑터에서 활동으로.

  • 다음 recycler view.for에 표시하려는 모델을 전달하면 모델이 다음 recyclerview 생성자를 통해 전달됩니다.

2) 다음 재활용 업체보기 어댑터에서 모델은 해당 모델의 값만 표시합니다.

참고 : 전화를 다시받는 방법을 알고 있음을 알립니다.

0

먼저 RecyclerView 어댑터를 선언하는 방법을 살펴야합니다. 이것은 매우 좋은 자습서입니다 : https://antonioleiva.com/recyclerview/

또한 목록의 각 요소에 태그를 설정해야합니다. Holder.itemView.setTag (HOLDER POSITION); 마지막으로 태그를 검색 할 수있는 클릭 리스너를 선언 할 수 있습니다.

대상 클래스에서 id = getIntent(). getExtras()를 호출하여 ID를 검색 할 수 있습니다.getInt ("ID");