Firebase RecyclerView not Displaying Retrieved Data in Fragment Despite Successful Fetch and Adapter Setup In kotlin Xml Android

I’m unable to display data retrieved from Firebase Realtime Database in a RecyclerView within my Android application.
Despite successfully fetching the data (as confirmed by logging), the RecyclerView remains empty when the fragment is displayed.
I have implemented a RecyclerView adapter (postadapter) with multiple view holders (TextViewHolder, ImageViewHolder, VideoViewHolder, ImageVideoViewHolder) to handle different types of post content (text, images, videos).
Adapter Debug Logs: Despite initializing the adapter and logging from within the adapter (Log.d statements), no logs are appearing in Logcat, suggesting potential issues with how the RecyclerView or adapter is being updated or rendered.
UI Rendering: The RecyclerView remains visually empty despite data being available and correctly fetched from Firebase.

I have implemented a RecyclerView adapter (communitypostadapter) with multiple view holders (TextViewHolder, ImageViewHolder, VideoViewHolder, ImageVideoViewHolder) to handle different types of post content (text, images, videos) and I’ve implemented a RecyclerView inside Fragment which is actually for Tabs and The Toast inside Fragment is showing so that Means Fragment is intialized but retriving values from firebase and display it in recylerview wont work. from the Log i got to understand retriving values is working. i’m unable to understand what’s the problem. i will provide relevant code and database structure.
PostFragment :

class postscommunityfragment : Fragment() {
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val view = inflater.inflate(R.layout.fragment_postscommunityfragment, container, false)

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

        return view
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        recyclerView.layoutManager = LinearLayoutManager(context)
        postList = mutableListOf()

        val name = arguments?.getString("name")
        Toast.makeText(context, name, Toast.LENGTH_LONG).show()

        val user = FirebaseAuth.getInstance().currentUser
        val userId = user?.uid

        databaseRef = FirebaseDatabase.getInstance().getReference("Posts").child(name!!).child(userId!!)

        databaseRef.addValueEventListener(object : ValueEventListener {
            override fun onDataChange(snapshot: DataSnapshot) {
                postList.clear()
                for (postSnapshot in snapshot.children) {
                    val post = postSnapshot.getValue(postdataclass::class.java)
                    post?.let { postList.add(it) }
                }
                // Initialize adapter and set it after fetching posts
                postAdapter = communitypostadapter(requireContext(), postList)
                recyclerView.adapter = postAdapter
                Log.d("postscommunityfragment", "Fetched ${postList.size} posts")
            }

            override fun onCancelled(error: DatabaseError) {
                Log.e("postscommunityfragment", "Database error: ${error.message}")
            }
        })
    }
}

Post Adapter :


class postadapter(private val context: Context, private val postList: List<postdataclass>) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    companion object {
        private const val TYPE_TEXT_ONLY = 0
        private const val TYPE_TEXT_IMAGE = 1
        private const val TYPE_TEXT_VIDEO = 2
        private const val TYPE_TEXT_IMAGE_VIDEO = 3
    }

    override fun getItemViewType(position: Int): Int {
        val post = postList[position]
        Log.d("communitypostadapter", "getItemViewType for position $position")
        return when {
            post.imguri != null && post.videouri != null -> TYPE_TEXT_IMAGE_VIDEO
            post.imguri != null -> TYPE_TEXT_IMAGE
            post.videouri != null -> TYPE_TEXT_VIDEO
            else -> TYPE_TEXT_ONLY
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        Log.d("communitypostadapter", "onCreateViewHolder for viewType $viewType")
        return when (viewType) {
            TYPE_TEXT_IMAGE -> ImageViewHolder(LayoutInflater.from(context).inflate(R.layout.communitypostimageitem, parent, false))
            TYPE_TEXT_VIDEO -> VideoViewHolder(LayoutInflater.from(context).inflate(R.layout.communitypostvideoitem, parent, false))
            TYPE_TEXT_IMAGE_VIDEO -> ImageVideoViewHolder(LayoutInflater.from(context).inflate(R.layout.communitypostvideoimageitem, parent, false))
            else -> TextViewHolder(LayoutInflater.from(context).inflate(R.layout.communityposttextitem, parent, false))
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val post = postList[position]
        Log.d("communitypostadapter", "onBindViewHolder for position $position")
        when (holder) {
            is TextViewHolder -> holder.bind(post)
            is ImageViewHolder -> holder.bind(post)
            is VideoViewHolder -> holder.bind(post)
            is ImageVideoViewHolder -> holder.bind(post)
        }
    }

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

    inner class TextViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private val postText: TextView = itemView.findViewById(R.id.text_view_post)
        private val username: TextView = itemView.findViewById(R.id.text_view_username)

        fun bind(post: postdataclass) {
            Log.d("TextViewHolder", "bind post: ${post.post}")
            postText.text = post.post
            username.text = post.username
        }
    }

    inner class ImageViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private val postText: TextView = itemView.findViewById(R.id.text_view_post)
        private val username: TextView = itemView.findViewById(R.id.text_view_username)
        private val postImage: ImageView = itemView.findViewById(R.id.image_view_post)

        fun bind(post: postdataclass) {
            Log.d("ImageViewHolder", "bind post: ${post.post}")
            postText.text = post.post
          
        }
    }

    //Also did created another two viewholder for videos and images
}

and Logs which are inside of Adapter Did not displayed and i created four viewholder as post can contain post text only or post text with many images or post text with many videos or post text with videos and images so that’s why i did created four viewholders and i dont know where is the issue inside fragment or adapter ? and i also tried send custom values to adapter if its working but it didnt worked.
I will also provide my Firebase Database structure :

{
  "CommunityPosts": {
    "name": {
      "ve7UmHpwqsuMeORHkKTffWWViZ8zd2": {
        "-O-_1nzOQs0hf7S5EOEw": {
          "currenttime": "currentimr",
          "post": "postdetails",
          "userimg": "uimgurl",
          "username": "username"
        },
        "-O-_2ES4itNTsmGccBye": {
          "currenttime": "currentime",
          "imguri": [
            "urls"
          ],
          "post": "post",
          "userimg": "uimgurl",
          "username": "username"
        },
        "-O-_2POg6ludb1x996FE": {
          "currenttime": "currenttime",
          "post": "post",
          "userimg": "userimg",
          "username": "username",
          "videouri": [
            "urls"
          ]
        },
        "-O-_2_URXwNDghuKoPrJ": {
          "currenttime": "currentime",
          "imguri": [
            "urls"
          ],
          "post": "post",
          "userimg": "",
          "username": "username",
          "videouri": [
            "urls"
          ]
        }
      }
    }
  }
}

I Added Structure so that it can help to understand my code. I dont know where and what’s the issue.

DATACLASS:

data class postdataclass(
    val post: String? = null,
    val imguri: List<String>? = null,
    val videouri: List<String>? = null,
    val username: String? = null,
    val userimg: String? = null,
    val currenttime: String? = null
)

Can we Use RecylerView Inside Fragment ? and From Logs I can Understand Adapter it didnt called so it still confuses me.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật