gpt4 book ai didi

firebase - RecyclerView不会从Firebase加载任何数据,并且所有行均显示为null

转载 作者:行者123 更新时间:2023-12-02 13:27:06 25 4
gpt4 key购买 nike

[在此处输入图片描述] [1]
尽管代码的格式和实现完全正确,但是应用程序中似乎存在一个错误,该错误不允许应用程序检索和显示数据。用于构建RecyclerView的后端的完整组件:
显示RecyclerView的片段:

package com.reazon.foodrunner.fragment

import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.firebase.ui.database.FirebaseRecyclerAdapter
import com.google.firebase.database.*
import com.reazon.foodrunner.R
import com.reazon.foodrunner.adapter.HomeRecyclerAdapter
import com.reazon.foodrunner.model.Restaurants

class HomeFragment : Fragment() {

private var mDatabaseReference: DatabaseReference? = null
private var recyclerAdapter: HomeRecyclerAdapter? = null
private var recyclerHome: RecyclerView? = null
internal var restaurantsList: MutableList<Restaurants> = ArrayList()
private var layoutManager: RecyclerView.LayoutManager? = null
private lateinit var adapter: FirebaseRecyclerAdapter<Restaurants,HomeRecyclerAdapter.HomeRecyclerViewHolder>
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
val view = inflater.inflate(R.layout.fragment_home, container, false)
mDatabaseReference = FirebaseDatabase.getInstance().getReference("Restaurants")
recyclerHome = view.findViewById(R.id.recyclerHomeAll)

layoutManager = LinearLayoutManager(activity)
recyclerHome!!.layoutManager = layoutManager

//retrieve data from firebase
retrieveAllRestaurants()

return view
}

private fun retrieveAllRestaurants() {
restaurantsList.clear()
val restaurantReference = FirebaseDatabase.getInstance().reference.child("Restaurants")

restaurantReference.addValueEventListener(object : ValueEventListener {
override fun onCancelled(error: DatabaseError) {
Log.d("ERROR ", "" + error.message)
}

override fun onDataChange(p0: DataSnapshot) {
for (snapshot in p0.children) {
val restaurant = snapshot.getValue(Restaurants::class.java)
restaurant!!.getRestaurant()
restaurant.getRestaurantRating()
restaurant.getCostForOne()
restaurant.getRestaurantName()

restaurantsList.add(restaurant)

}
recyclerAdapter = HomeRecyclerAdapter(context!! , restaurantsList)
recyclerHome?.adapter = recyclerAdapter
}
})
}
}
使用的模型类:

模型类
package com.reazon.foodrunner.model

class Restaurants {
private var id: String? = null
private var name: String? = null
private var rating: String? = null
private var cost_for_one: String? = null
private var imageUrl: String? = null

constructor()

constructor(
id: String,
name: String,
rating: String,
cost_for_one: String,
imageUrl: String
) {
this.id = id
this.name = name
this.rating = rating
this.cost_for_one = cost_for_one
this.imageUrl = imageUrl
}

fun getRestaurant(): String? {
return imageUrl
}

fun setRestaurant(imageUrl: String){
this.imageUrl = imageUrl
}

fun getRestaurantId(): String? {
return id
}

fun setRestaurantId(id: String){
this.id = id
}

fun getRestaurantName(): String? {
return name
}

fun setRestaurantName(name: String){
this.name = name
}
fun getRestaurantRating(): String? {
return rating
}
fun setRestaurantRating(rating: String){
this.rating = rating
}

fun getCostForOne(): String? {
return cost_for_one
}
fun setCostForOne(cost_for_one: String) {
this.cost_for_one = cost_for_one
}
}
使用的适配器类:
适配器类
package com.reazon.foodrunner.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageButton
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.recyclerview.widget.RecyclerView
import com.reazon.foodrunner.R
import com.reazon.foodrunner.model.Restaurants
import com.squareup.picasso.Picasso

class HomeRecyclerAdapter(
var context: Context,
var restaurantsList: List<Restaurants>
) : RecyclerView.Adapter<HomeRecyclerAdapter.HomeRecyclerViewHolder>() {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HomeRecyclerViewHolder {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.recycler_home_single_row, parent, false)
return HomeRecyclerViewHolder(view)
}

override fun getItemCount(): Int {
return restaurantsList.size
}

override fun onBindViewHolder(holder: HomeRecyclerViewHolder, position: Int) {
val restaurant = restaurantsList[position]
holder.costForOne.text = restaurant.getCostForOne().toString()
holder.rating.text = restaurant.getRestaurantRating().toString()
holder.textView.text = restaurant.getRestaurantName()
Picasso.get().load(restaurant.getRestaurant()).placeholder(R.drawable.food_runner_icon).into(holder.imageView).toString()
holder.btnFavourite.setOnClickListener {
Toast.makeText(context ,"Favourite Button Clicked",Toast.LENGTH_LONG).show()
}
}

class HomeRecyclerViewHolder(view: View) : RecyclerView.ViewHolder(view) {
var imageView: ImageView = view.findViewById(R.id.imgRestaurantImage)
var costForOne: TextView = view.findViewById(R.id.txtCostForOne)
var textView: TextView = view.findViewById(R.id.txtRestaurantName)
var btnFavourite: ImageButton = view.findViewById(R.id.btnFavourite)
var rating: TextView = view.findViewById(R.id.txtRating)
}
}
Firebase数据库结构:
[1]: /image/XDrxa.png

最佳答案

Although the code is formatted and implemented completely fine, there seems to a bug in the application that doesn't allow the app to retrieve and display the data.


代码中的主要问题在于 Restaurants类,其中所有 getter 的名称都不正确。如果您的类(class)中有一个名为 id的属性,则对应的getter应该是 getId()而不是 getRestaurantId()。另一方面,setter应该是 setId()而不是 setRestaurantId()。有两种方法可以解决此问题。第一个是根据上面的示例更改所有 getter / setter 的所有名称。
另外请注意,setter和getter并不是强制性的。 setter 始终是可选的,因为如果没有JSON属性的 setter ,Firebase客户端将直接在字段上设置值。但是,创建此类的一种更简单的方法是定义属性并将其直接初始化到构造函数中,然后将该类设置为 data class:
data class Restaurant(
var id: String? = null,
var name: String? = null,
var rating: String? = null,
var cost_for_one: String? = null,
var imageUrl: String? = null
)
另请注意,该类的名称是 Restaurant而不是 Restaurants,这是因为可以将此类视为单个餐厅的蓝图,而不是更多餐厅的蓝图。

关于firebase - RecyclerView不会从Firebase加载任何数据,并且所有行均显示为null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63239393/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com