我是 Android Studio 的新手。我一直在关注有关如何使用 Firebase 创建基于 Kotlin 的 MVVM 应用程序的教程。我创建了一个名为 ServiceOrder 的数据类,其中包含我的应用程序所需的所有数据字段。
我想列出 RecyclerView 中的所有 ServiceOrder 对象。我创建了一个链接到适配器 (SOListingAdapter) 的 SOListingFragment。适配器类管理数据并将其绑定到 RecyclerView。
运行应用程序时,我已经能够将数据添加到 Firebase,但列表布局未显示 Firestore 数据库中的任何数据
这是我所期待的:预期布局
这是运行时的结果:结果布局在此处
有人可以帮我确定问题出在哪里吗?我已经坚持这个问题好几个星期了。
data class ServiceOrder(
val id: String="",
val SO_numb: String="",
val SO_title: String="",
val SO_type: String="",
val SO_description: String="",
@ServerTimestamp
val date: Date= Date(),)
@AndroidEntryPoint class SOListingFragment : Fragment() {
val TAG: String = "SOListingFragment"
lateinit var binding: FragmentSOListingBinding
val viewModel: ServiceOrderViewModel by viewModels()
val adapter by lazy {
SOListingAdapter(
onItemClicked = { pos, item -> } ,
onEditClicked = { pos, item -> } ,
onDeleteClicked = {pos, item ->},
)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
// Inflate the layout for this fragment
//return inflater.inflate(R.layout.fragment_s_o_listing, container, false)
binding = FragmentSOListingBinding.inflate(layoutInflater)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.recyclerView.adapter = adapter
binding.button.setOnClickListener{
findNavController().navigate(R.id.action_SOListingFragment_to_SODetailFragment)
}
viewModel.getServiceOrders()
viewModel.serviceOrder.observe(viewLifecycleOwner){ state ->
when(state){
is UiState.Loading -> {
binding.progressBar.show()
}
is UiState.Failure -> {
binding.progressBar.hide()
toast(state.error)
}
is UiState.Success->{
binding.progressBar.hide()
adapter.updateList(state.data.toMutableList())
}
}
}
}
class SOListingAdapter (
val onItemClicked:
(Int, ServiceOrder)-> Unit,
val onEditClicked: (Int, ServiceOrder)-> Unit,
val onDeleteClicked: (Int, ServiceOrder)-> Unit,
): RecyclerView.Adapter<SOListingAdapter.MyViewHolder>(){private var list: MutableList<ServiceOrder> = arrayListOf()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
val itemView = ItemSoLayoutBinding.inflate(LayoutInflater.from(parent.context),parent,false)
return MyViewHolder(itemView)
}
fun updateList(list: MutableList<ServiceOrder>){
this.list = list
notifyDataSetChanged()
}
override fun getItemCount(): Int {
return list.size
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
val item = list[position]
Log.d("SOListingAdapter", "Binding item at position $position: $item")
holder.bind(item)
}
inner class MyViewHolder(val binding: ItemSoLayoutBinding): RecyclerView.ViewHolder(binding.root) { fun bind(item: ServiceOrder){ binding.SoNumber.setText(item.SO_numb)
binding.SoType.setText(item.SO_type)
binding.SoDescription.setText(item.SO_description)
binding.SoLayout.setOnClickListener{ onEditClicked.invoke(adapterPosition,item)}
binding.itemLayout.setOnClickListener{onItemClicked.invoke(adapterPosition,item)}
}
}
}
我使用 Kotlin 和 Hilt 进行依赖注入,在 Android 应用程序中实现了 SOListingFragment。该片段旨在使用带有自定义适配器 (SOListingAdapter) 的 RecyclerView 显示服务订单列表。运行应用程序时,我无法查看列表菜单上的数据
我的数据库结构为 JSON 文件。
{
"collections":{
"serviceorder":{
"OInkAxPraLS4WgtqmzEV":{
"date":{
"datatype":"timestamp",
"value":{
"_seconds":1719104689,
"_nanoseconds":372000000
}
},
"so_description":"ok my man",
"so_numb":"140678",
"so_type":"Inspection",
"so_title":"No Power",
"id":"OInkAxPraLS4WgtqmzEV",
"collections":{
}
},
"sFxKZZnx5V6RGeoBGwgd":{
"date":{
"datatype":"timestamp",
"value":{
"_seconds":1719107360,
"_nanoseconds":29000000
}
},
"so_description":"aeet",
"so_numb":"131493",
"so_type":"Inspection",
"so_title":"Maintenance",
"id":"sFxKZZnx5V6RGeoBGwgd",
"collections":{
}
},
"y6P2kSN7pee86nmVVSF1":{
"date":{
"datatype":"timestamp",
"value":{
"_seconds":1719773956,
"_nanoseconds":425000000
}
},
"so_description":"free to go man ",
"so_numb":"143623",
"so_type":"Fraud Case",
"id":"y6P2kSN7pee86nmVVSF1",
"so_title":"Inspection",
"collections":{
}
}
}
}
}
代码中的问题在于数据库中的字段名称与类中的字段名称不匹配。我在
ServiceOrder
类中看到一个名为 SO_numb
的字段,而在数据中是 so_numb
。看到大写字母 SO 和小写字母 so 之间的区别了吗?要解决此问题,您可以更改类中字段的名称以匹配数据库中的名称,也可以使用 PropertyName 注释,如我在以下帖子中的回答中所述: