无法在片段的回收视图中获取实时数据库

Unable to get realtime database in recyclerview of the fragment

我正在尝试从实时数据库的片段的回收视图中获取数据。

这是我用来从实时数据库获取数据的Home Fragment代码:-

class HomeFragments : Fragment() {

    private var recyclerView: RecyclerView? = null
    private var typesAdapter: SU_Types_adapter? = null
    private var type: MutableList<SU_Type_Model>? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        recyclerView = rv_home_fragment
        recyclerView?.setHasFixedSize(true)
        recyclerView?.layoutManager = GridLayoutManager(activity, 2)

        type = ArrayList()
        typesAdapter = activity?.let { SU_Types_adapter(it, type as ArrayList<SU_Type_Model>, true) }
        recyclerView?.adapter = typesAdapter

        val typesRef = FirebaseDatabase.getInstance().getReference().child("SU_Types").orderByChild("type")
        typesRef.addValueEventListener(object : ValueEventListener {
            override fun onDataChange(dataSnapshot: DataSnapshot) {
                for (snapshot in dataSnapshot.children) {
                    typesAdapter?.notifyDataSetChanged()
                    val su_type = snapshot.getValue(SU_Type_Model::class.java)
                    if (su_type != null) {
                        (type as ArrayList<SU_Type_Model>)?.add(su_type)
                    }
                }
            }

            override fun onCancelled(error: DatabaseError) {

            }
        })
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_home_fragments, container, false)
    }
}

这是我用来获取数据库数据的适配器代码:-

class SU_Types_adapter(
    private var mContext: Context,
    private var mTypes: List<SU_Type_Model>,
    private var isActivity: Boolean = false
) : RecyclerView.Adapter<SU_Types_adapter.ViewHolder>() {

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

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val su_type = mTypes[position]

        Picasso.get().load(su_type.getImageUrl()).into(holder.ImageIv)
        holder.ImgId.text = su_type.gettype()
    }

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

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        var ImageIv: ImageView? = itemView.su_type_iv
        var ImgId: TextView = itemView.su_type_tv

    }
}

型号代码如下:-

class SU_Type_Model {

    private var ImageUrl: String = ""
    private var type: String = ""

    constructor()

    constructor(
        ImageUrl: String,
        type: String
    ) {
        this.ImageUrl = ImageUrl
        this.type = type
    }


    fun getImageUrl(): String {
        return ImageUrl
    }

    fun setImageUrl(ImageUrl: String) {
        this.ImageUrl = ImageUrl
    }

    fun gettype(): String {
        return type
    }

    fun settype(type: String) {
        this.type = type
    }
}

这里是[数据库映像]的快照。]

json 文件代码为:-

{
  "SU_Types": {
    "asdfadsfa": {
      "ImageUrl": "\"https://firebasestorage.googleapis.com/v0/b/ehi-10.appspot.com/o/Types%2FAdvertising%2F1113-lalbaugcha-raja-wallpaper-07.jpg?alt=media&token=c2d78337-e66b-43cf-bed5-8e65cf94c342\"",
      "type": "\"sing\""
    }
  }
}

我能够使用类似的代码在 activity 中获取数据,但我无法在 Fragment 中获取数据。有人可以帮我吗?

而不是 activity 使用上下文:-

  recyclerView?.layoutManager = GridLayoutManager(context, 2)

typesAdapter = context?.let { SU_Types_adapter(it, type as ArrayList<SU_Type_Model>, true) }

并进行更改以获取 recyclerview:-

    recyclerView = view.findViewById(R.id.rv_home_fragment)