【发布时间】:2021-12-19 10:01:38
【问题描述】:
我有一个验证产品的功能,正如您在这个 sn-p 中看到的那样:
private fun validateProduct() : Boolean{
val newProductName = binding.etNewProductName.text.toString().trim()
val newProductPrice = binding.etNewProductPrice.text.toString().trim()
val newProductCategory = binding.spNewProductCategory.selectedItem.toString()
return when{
TextUtils.isEmpty(newProductName) -> {
showErrorSnackBar(binding.root, "Product name cannot be empty.", true)
false
}
TextUtils.isEmpty(newProductPrice) -> {
showErrorSnackBar(binding.root, "Price cannot be empty.", true)
false
}
//make sure the first element is not a valid category
newProductCategory == binding.spNewProductCategory.getItemAtPosition(0) -> {
showErrorSnackBar(binding.root, "Please select a valid category.", true)
false
}
//check if the new product's name already exists in the Firestore collection.
//if so, return false.
else -> {
true
}
}
}
编辑: 我的逻辑是遍历文档。检查每个文档 if document["name"].toString() == newProductName 如果是,则返回 false 并显示错误小吃栏。
【问题讨论】:
标签: android firebase kotlin google-cloud-platform google-cloud-firestore