簡體   English   中英

無法將數據從 recyclerview 適配器傳遞到另一個 kotlin 片段

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

我正在嘗試從活動轉移到片段

在舊的活動應用程序中,當用戶單擊項目時,他將使用項目數據進行新活動

現在我在片段中需要同樣的東西

這是我的適配器代碼

    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)
        */
    }
}

當我單擊該項目時,我收到此錯誤

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)

對不起我的英語不好

提前致謝

主要問題是您正在獲取應用程序上下文並將其轉換為 MainActivity 這是不可能的,因為名稱聲明它只是應用程序上下文。 實際上,當您想要使用片段時,唯一負責將片段保存在內部的組件是活動實例。 因此,您唯一需要做的就是替換以下代碼行:

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

使用以下代碼行:

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

然后您的代碼將正常工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM