0

I am trying to call an activity called playerActivity from one of my fragment called Homefragment which uses an recycler view. I have created an interface to communicate between my fragment and activity but when i click on the recyclerview list it does not go to the activity.

class MusicAdapter(private val context: Homefragment, private val musicList: ArrayList<Music>, var listener:OnItemClick? = null) : RecyclerView.Adapter<Musicholder>() {
    class Musicholder(binding: SongviewBinding) : RecyclerView.ViewHolder(binding.root) {
        val title = binding.songname
        val album = binding.albumname
        val songimage = binding.songicon
        val songduration = binding.duration
        val root = binding.root

    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MusicAdapter.Musicholder {
     return Musicholder(SongviewBinding.inflate(LayoutInflater.from(parent.context),parent, false))

    }

    override fun onBindViewHolder(holder: MusicAdapter.Musicholder, position: Int) {
       holder.title.text = musicList[position].title
       holder.album.text = musicList[position].album
        holder.songduration.text = formatduration(musicList[position].duration)
        Glide.with(context)
            .load(musicList[position].artUri)
            .placeholder(R.drawable.music_icon_splashscreen).centerCrop()
            .into(holder.songimage)
         holder.root.setOnClickListener{
             listener?.onItemclicked(musicList[position])

        }

    }


    override fun getItemCount(): Int {
      return musicList.size
    }
}

the fragment code

class Homefragment : Fragment() , OnItemClick{

    private lateinit var musicAdapter:MusicAdapter
    private lateinit var binding: FragmentHomefragmentBinding

    companion object{
        lateinit var MusicListFA: ArrayList<Music>
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
      val binding = FragmentHomefragmentBinding.inflate(inflater, container, false)
        binding.Musicrecycler.setHasFixedSize(true)
        binding.Musicrecycler.setItemViewCacheSize(20)
        binding.Musicrecycler.layoutManager = LinearLayoutManager(context)
        MusicListFA = getAllAudio()
        musicAdapter = MusicAdapter(this, MusicListFA)
        binding.Musicrecycler.adapter = musicAdapter
        return binding.root
    }

    @SuppressLint("Range")
    private fun getAllAudio() : ArrayList<Music>{
        val tempList = ArrayList<Music>()
        val selection = MediaStore.Audio.Media.IS_MUSIC+ "!=0" // to check the file is not null
        val projection = arrayOf(MediaStore.Audio.Media._ID, MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.ALBUM,
            MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.DURATION,MediaStore.Audio.Media.DATA,MediaStore.Audio.Media.DATE_ADDED,MediaStore.Audio.Media.ALBUM_ID)
        val cursor = context?.contentResolver?.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,projection,selection,null,MediaStore.Audio.Media.DATE_ADDED + " DESC",null)
        if (cursor != null){
            if(cursor.moveToFirst())
                do {
                    val titlec = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE))
                    val albumc = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM))
                    val idc = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media._ID))
                    val artistc = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST))
                    val pathc = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA))
                    val durationc = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION))
                    val albumidc = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID)).toString()
                    val uri = Uri.parse("content://media/external/audio/albumart")
                    val artUric = Uri.withAppendedPath(uri, albumidc).toString()
                    val music = Music(title = titlec, album = albumc, artist = artistc, id = idc, path = pathc, duration = durationc, artUri = artUric)
                    val file = File(music.path)
                    if(file.exists())
                        tempList.add(music)

                }while (cursor.moveToNext())
                cursor.close()
        }

        return tempList
    }

    override fun onItemclicked(music: Music) {
        super.onItemclicked(music)

        val intent = Intent(context, Playeractivity::class.java)
        ContextCompat.startActivity(context!!, intent,null)
    }
}

the interface class

interface OnItemClick {
    fun onItemclicked(music: Music){

    }
}

1 Answers1

0

You haven't pass in the listener.

Change

musicAdapter = MusicAdapter(this, MusicListFA)

to

musicAdapter = MusicAdapter(this, MusicListFA, this)

Ricky Mo
  • 6,285
  • 1
  • 14
  • 30
  • Thank you! Now I am able to call the activity. – Abhinav Sai May 18 '22 at 09:58
  • How can i get the position of the song from adapter to fragment to pass it to the activity? – Abhinav Sai May 18 '22 at 10:38
  • Please refer to this post [https://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android-application](https://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android-application) – Ricky Mo May 18 '22 at 14:33
  • In the above adapter i have set the onclicklistener to the musicList[position], Now how do i get the position of the itemclick in fragment to pass it to my activity through intent. As it will not allow me to pass the position and is asking to create a local variable again for the position. please suggest!!! – Abhinav Sai May 19 '22 at 08:43