【发布时间】:2021-11-21 17:35:32
【问题描述】:
我有两个 Spinner,每个都由 ArrayList 填充,在 Fragment 的 ViewModel 中观察到如下:
InventoryAddEdit 片段
// Observe ProductGroups and populate Spinner
businessViewModel.allAppDataProductGroups.observe(viewLifecycleOwner, { productGroupArrayList -> // ArrayList<ProductGroupObject>
if (!productGroupArrayList.isNullOrEmpty()){
val adapter = ArrayAdapter(requireContext(), android.R.layout.simple_spinner_dropdown_item, productGroupArrayList)
binding.inventoryAddEditProductGroupSpinner.adapter = adapter
}
})
// Observe ProductTypes and populate Spinner
businessViewModel.allAppDataProductTypes.observe(viewLifecycleOwner, { productTypeArrayList -> // ArrayList<ProductTypeObject>
if (!productTypeArrayList.isNullOrEmpty()){
val adapter = ArrayAdapter(requireContext(), android.R.layout.simple_spinner_dropdown_item, productTypeArrayList)
binding.inventoryAddEditProductTypeSpinner.adapter = adapter
binding.inventoryAddEditProductTypeSpinner.setSelection(17) // Sets default value
}
})
我试图避免将数据保留在 Fragment (MVVM) 中,所以我想知道如何根据第一个 ArrayList 的选择最好地过滤第二个 ArrayList。
我想我可以在第一个微调器上使用onItemSelected 方法来取消观察者并重新附加,然后通过选择第一个微调器来过滤新观察到的 ArrayList。但是,这似乎有点笨拙。另一个想法是在 ViewModel 中创建另一个过滤列表,但这意味着 ViewModel 中有更多数据。
请问还有其他选择吗?
有关信息,ProductGroupObject 和 ProductTypeObject 如下所示:
ProductGroupObject
@IgnoreExtraProperties
data class ProductGroupObject (
//ProductGroup fields (1 fields)
var productGroup: String = "",
@ServerTimestamp
var dateEditedTimestamp: Date? = null,
@Exclude @set:Exclude @get:Exclude
var productGroupID: String = ""
) : Serializable {
override fun toString(): String {
return productGroup
}
}
ProductTypeObject
@IgnoreExtraProperties
data class ProductTypeObject (
//ProductType fields (2 fields)
var productType: String = "",
var productGroup: String = "",
@ServerTimestamp
var dateEditedTimestamp: Date? = null,
@Exclude @set:Exclude @get:Exclude
var productTypeID: String = ""
) : Serializable {
override fun toString(): String {
return productType
}
fun detailsText(): String {
val detailsString = StringBuilder()
if(productTypeID.isNotEmpty()) detailsString.append("$productTypeID\n")
if(productType.isNotEmpty()) detailsString.append("$productType\n")
if(productGroup.isNotEmpty()) detailsString.append("$productGroup\n")
return detailsString.toString()
}
}
【问题讨论】:
标签: kotlin arraylist android-spinner android-livedata