簡體   English   中英

在 kotlin android studio 中的片段之間傳遞數據

[英]Pass data between fragments in kotlin android studio

我想將一些數據從 PowerofMind 傳遞到願望清單片段,但遇到了一些錯誤。

此活動必須從中傳輸數據

      wish?.setOnClickListener({
            val name = "Power of Subconcoius Mind"
            val intent = Intent(this@PowerofMind, WishlistFragment::class.java)
            intent.putExtra("Book: ", name)
            startActivity(intent)
            Toast.makeText(this, "Added to WishList", Toast.LENGTH_SHORT).show()
        })

我想將此活動中的數據顯示為



class WishlistFragment : Fragment() {
    var result: TextView? = null

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val view = inflater.inflate(R.layout.fragment_wishlist, null)
        result =view.findViewById(R.id.list1)
        val name = activity?.intent?.getStringExtra("Book: ")
        list1.text = "Book: $name"
        return view
    }
}

但是在 Intent 上有一個錯誤。 請幫忙

以下是如何使用工廠方法實例化片段的示例:

companion object {
    private const val MY_DATA_KEY = "my_data"
    private const val ANOTHER_DATA_KEY = "another_data"
    fun newInstance(mySerializableData: Any, anotherData: Int) = MyFragment().apply {
        //bundleOf() is an exstension method from KTX https://developer.android.com/kotlin/ktx
        arguments = bundleOf(MY_DATA_KEY to mySerializableData, ANOTHER_DATA_KEY to anotherData)
    }
}

以下是使用 Parcelable 類在 kotlin 中的片段之間傳遞數據的方法:

在按鈕上單擊:

     override fun onClick(v: View?) {
        firstName = editTextName!!.text.toString()
        lastName = editTextLast!!.text.toString()
        Toast.makeText(context, firstName, Toast.LENGTH_SHORT).show()
//        val viewFragment = ViewFragment()
//        val transaction = fragmentManager.beginTransaction()
//        transaction.replace(R.id.fragmentContainer, viewFragment)
//        transaction.commit()

        var details = Details(firstName!!, lastName!!)
        val viewFragment = ViewFragment()
        val bundle = Bundle()
        bundle.putParcelable(KEY_PARSE_DATA, details)
        viewFragment.setArguments(bundle)
        val transaction = fragmentManager.beginTransaction()
        transaction.replace(R.id.fragmentContainer, viewFragment)
        transaction.commit()
    }

這是一個包裹類如何處理數據

     @Parcelize
class Details(val firstName: String, val lastName: String) : Parcelable

在另一個片段上

override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        val view: View = inflater!!.inflate(R.layout.fragment_view, container, false)

        textViewName = view.findViewById(R.id.text_name_another) as TextView
        textViewLastName = view.findViewById(R.id.text_surname_another) as TextView

        val bundle = arguments
        if (bundle != null) {
            val details = bundle.getParcelable<Details>(KEY_PARSE_DATA)
            textViewName!!.setText(details.firstName)
            textViewLastName!!.setText(details.lastName)
        }


        return view
    }

目前我不知道在應用程序 gradle 中的 kotlin 中是否需要這樣做(使用前請檢查)

androidExtensions {
    experimental = true
}

在您的 WishlistFragment 中,您正在創建一個新的 Intent,而不是獲取活動提供的 Intent。

當您使用 Kotlin 時,您可以直接使用 Intent 而不是 getIntent()。 您還可以使用 kotlin 的綜合優勢,並刪除 findViewById。

作為提示,不要使用字符串連接 ;)

您的片段將類似於以下內容:

class WishlistFragment : Fragment() {

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    val view = inflater.inflate(R.layout.fragment_wishlist, null)
    val name = activity?.intent?.getStringExtra("Book: ")
    many.text = "Book: $name"
}

暫無
暫無

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

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