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

axr492tv  于 2022-11-14  发布在  Android
关注(0)|答案(1)|浏览(163)

我正在尝试从实时数据库中获取片段的recyclerview中的数据。
下面是我用来从实时数据库中获取数据的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
    }
}

下面是[数据库映像.] x1c 0d1x快照
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中的数据。有人能帮助我吗?

0g0grzrc

0g0grzrc1#

使用上下文代替活动:-

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)

相关问题