android - 如何在 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)
}
}
解决方案
推荐阅读
- java - 等到状态出现在 Java for 循环中
- php - 如何在php中将字符串转换为日期
- bash - 将具有相同键的变量添加到数组时枚举?
- python - Python Django:填写表单字段总是在POST上抛出错误“此字段是必需的”
- powershell - 无法将“System.String”类型的“/Action:”值转换为“System.Management.Automation.ScriptBlock”类型
- python - 从python中的路径获取子文件名而不使用split
- r - 如何使用 R 中的 ggplot2 在任何给定时间绘制多个数量的平均值?
- css - 使用 css 网格显示项目并构建第一个固定列
- javascript - 更新相同键的数组值
- python - 在 apply_async 中更新 db 模型不执行