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.