简体   繁体   中英

Can't pass data from recyclerview adapter to another kotlin fragment

I'm trying to move from activities to fragments

In old activity app when the user clicks on the item he is going to a new activity with item data

Now i need same thing in fragment

Here's my adapter code

    override fun onBindViewHolder(holder: PhotosHolder, position: Int) {
        val productPhoto = photosArrayList[position]
        val key = productPhoto.key
        val name = productPhoto.name
        val price = productPhoto.price
        val photo = productPhoto.photo
        val photo2 = productPhoto.photo2
        val photo3 = productPhoto.photo3
        val link = "http://suleimanmf.com/Photos/"

        holder.key.text = key
        holder.name.text = name
        holder.price.text = price
        holder.photo = photo
        holder.photo2 = photo2
        holder.photo3 = photo3
        holder.container.setOnClickListener {
            goToProductInfo(productPhoto)
        }
    }

    private fun goToProductInfo(info: Photo) {

        val photosFragment = PhotosFragment()
        val bundle = Bundle()

        bundle.putString("key", info.key)
        bundle.putString("name", info.name)
        bundle.putString("price", info.price)
        bundle.putString("photo", info.photo)
        bundle.putString("photo2", info.photo2)
        bundle.putString("photo3", info.photo3)

        photosFragment.arguments = bundle

        (context.applicationContext as MainActivity).supportFragmentManager.beginTransaction()
            .apply {
                replace(R.id.nav_host_fragment_content_main, photosFragment).addToBackStack(null)
                commit()
            }
        // Old activity
        /**
        val intent = Intent(context, ProductInfoActivity::class.java)
        intent.putExtra("key", info.key)
        intent.putExtra("name", info.name)
        intent.putExtra("price", info.price)
        intent.putExtra("photo", info.photo)
        intent.putExtra("photo2", info.photo2)
        intent.putExtra("photo3", info.photo3)
        startActivity(context, intent, null)
        */
    }
}

When I click on the item I get this error

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.suleimanmf.gazarcustomers, PID: 5389
    java.lang.ClassCastException: android.app.Application cannot be cast to androidx.appcompat.app.AppCompatActivity
        at com.suleimanmf.gazarcustomers.ui.gallery.adapter.PhotosAdapter.goToProductInfo(PhotosAdapter.kt:97)
        at com.suleimanmf.gazarcustomers.ui.gallery.adapter.PhotosAdapter.onBindViewHolder$lambda-0(PhotosAdapter.kt:80)

Sorry for my poor English

Thanks in advance

The main problem is that you are getting the application context and casting it to MainActivity which is not possible because as the name declares it's application context only. Actually when you want to work with fragments, the only component which is rrsponsible to hold the fragments inside, is the activity instance. So, the only thing you need to do is replacing the below code lines:

(context.applicationContext as MainActivity).supportFragmentManager.beginTransaction()
        .apply {
            replace(R.id.nav_host_fragment_content_main, photosFragment).addToBackStack(null)
            commit()
        }

With the below lines of code:

(context as MainActivity).supportFragmentManager.beginTransaction()
        .apply {
            replace(R.id.nav_host_fragment_content_main, photosFragment).addToBackStack(null)
            commit()
        }

Then your code will work properly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM