首页 > 解决方案 > 如何在 onStart() 中添加函数 | 科特林

问题描述

我有一个问题,因为我试图向 onStart () 添加一个 onReceiptsAdd () 函数,但我不知道如何使它工作。如何将 onReceiptsAdd () 添加到 onStart () 这是我的代码:

class ScanFragment : Fragment(), OnReceiptsItemAdd {
    private var _binding: FragmentScanBinding? = null
    private val binding get() = _binding!!

    private val scanVm by viewModels<ScanViewModel>()
    private val adapter = ReceiptsAdapter(this)

    private val SCAN_DEBUG = "SCAN_DEBUG"

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?,
    ): View {
        _binding = FragmentScanBinding.inflate(inflater, container, false)
        // Inflate the layout for this fragment
        return binding.root
    }
    override fun onStart() {
        super.onStart()

        val value = binding.etBlank.text?.trim().toString()
        val from = binding.etBlank.text?.trim().toString()
        val image = binding.etBlank.text?.trim().toString()
        val rootRef = FirebaseFirestore.getInstance()
        val usersRef = rootRef.collection("receipts")
        val auth = FirebaseAuth.getInstance()
        auth.currentUser?.apply {
            usersRef.document(uid).set(mapOf(
                "id" to uid,
                "from" to from,
                "value" to value,
                "image" to image,
            )).addOnCompleteListener { task ->
                if (task.isSuccessful) {
                    Log.d("TAG", "User successfully added.")
                } else {
                    Log.d("TAG", task.exception!!.message!!)
                }
            }
        }
    }
    override fun onReceiptsAdd(receipts: dataReceipts, position: Int) {
        scanVm.addFavReceipt(receipts)
    }
}

标签: androidkotlinmvvmfragmentonstart

解决方案


推荐阅读