Home > Back-end >  Unable to get realtime database in recyclerview of the fragment
Unable to get realtime database in recyclerview of the fragment

Time:05-02

I am trying to get data in recyclerview of fragment from realtime database.

Here is the code of Home Fragment I used to get data from realtime database:-

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)
    }
}

Here is the adapter code that I use to get the database data:-

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

    }
}

Here is the model code:-

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
    }
}

here is snapshot of [database image.]database image

json file code is:-

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

I was able to get the data in activity using similar code but I am unable to get the data in Fragment. Can someone please help me?

CodePudding user response:

Instead of activity use context:-

  recyclerView?.layoutManager = GridLayoutManager(context, 2)

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

and make changes for getting recyclerview:-

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