Home > Blockchain >  Recycler View doesn't show anything
Recycler View doesn't show anything

Time:10-07

list_users.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/user_list_bg"
        android:layout_margin="10dp">

    <de.hdodenhof.circleimageview.CircleImageView
        android:id="@ id/user_profile_image_view"
        android:layout_width="90sp"
        android:layout_height="90sp"
        android:src="@drawable/user_image_placeholder"
        android:layout_margin="12dp"
        app:border_width="1sp"
        android:clickable="true"
        android:focusable="true">

    </de.hdodenhof.circleimageview.CircleImageView>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:orientation="vertical">
        <TextView
            android:id="@ id/user_name_tv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Hardik Dhuri"
            android:layout_marginBottom="10dp"
            android:textSize="24sp"/>

        <TextView
            android:id="@ id/last_message_tv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Hello Bro"
            android:textSize="16sp"/>
    </LinearLayout>

    </LinearLayout>

fragment_chats.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".main.fragments.ChatsFragment"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <androidx.recyclerview.widget.RecyclerView
        android:id="@ id/list_users_rv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:listitem="@layout/list_users"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"/>
</FrameLayout>

ListUsersAdapter.kt

package com.example.samvach.adapters

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.samvach.R
import com.example.samvach.models.User

class ListUsersAdapter(private val userList: ArrayList<User>) : RecyclerView.Adapter<ListUsersAdapter.MyViewHolder>() {

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

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        val item = userList[position]
        holder.name.text = item.name
    }

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

    class MyViewHolder(itemView: View): RecyclerView.ViewHolder(itemView) {
        val name: TextView = itemView.findViewById(R.id.user_name_tv)
    }
}

ChatsFragment.kt

Here I am getting data from firebase. I checked the data by logging it and it seems fine only thing i can't figure out is where is the actual error in my recycler view. I create few users manually to test and they don't show up either.

package com.example.samvach.main.fragments

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.RecyclerView
import com.example.samvach.R
import com.example.samvach.adapters.ListUsersAdapter
import com.example.samvach.databinding.FragmentChatsBinding
import com.example.samvach.models.User
import com.google.firebase.database.*


class ChatsFragment : Fragment() {

    private lateinit var binding: FragmentChatsBinding
    private lateinit var dbref: DatabaseReference
    private lateinit var usersArrayList: ArrayList<User>

    companion object {
        fun newInstance() = ChatsFragment()
    }

    private lateinit var viewModel: ChatsViewModel

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_chats, container, false)
    }


    @Deprecated("Deprecated in Java")
    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        viewModel = ViewModelProvider(this)[ChatsViewModel::class.java]
        binding = FragmentChatsBinding.inflate(layoutInflater)

        binding.listUsersRv.setHasFixedSize(true)
        usersArrayList = arrayListOf(
            User(
                name = "Banana"
            ),
            User(
                name = "Apple"
            ),
            User(
                name = "Kiwi"
            ),
            User(
                name = "Orange"
            )
        )
        binding.listUsersRv.adapter = ListUsersAdapter(usersArrayList)
//        getUserData()
    }

    private fun getUserData() {
        dbref = FirebaseDatabase.getInstance().getReference("users")

        dbref.addValueEventListener(object : ValueEventListener {
            override fun onDataChange(snapshot: DataSnapshot) {
                usersArrayList.clear()
                if (snapshot.exists()) {
                    for (userSnapshot in snapshot.children) {
                        val user = userSnapshot.getValue(User::class.java)
                        Log.i("USER", "${user!!.name}")
                        usersArrayList.add(user)
                    }
                    binding.listUsersRv.adapter = ListUsersAdapter(usersArrayList)
                }
            }

            override fun onCancelled(error: DatabaseError) {
                TODO("Not yet implemented")
            }

        })

    }

}

User.kt

package com.example.samvach.models

data class User (
    var email: String?= null,
    var name: String?= null,
    var profilePicture: String?= null,
    var uid: String?= null,

)

CodePudding user response:

You have an error in binding = FragmentChatsBinding.inflate(layoutInflater), try to init your binding in onCreateView like this:

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    val view = inflater.inflate(R.layout.fragment_chats, container, false)
    binding = FragmentChatsBinding.bind(view)
    return binding.root
}

or move your code of onActivityCreated to onViewCreated and use view instance

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    return inflater.inflate(R.layout.fragment_chats, container, false)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    binding = FragmentChatsBinding.bind(view)
    viewModel = ViewModelProvider(this)[ChatsViewModel::class.java]
    binding.listUsersRv.setHasFixedSize(true)
    usersArrayList = arrayListOf(
        User(
            name = "Banana"
        ),
        User(
            name = "Apple"
        ),
        User(
            name = "Kiwi"
        ),
        User(
            name = "Orange"
        )
    )
    binding.listUsersRv.adapter = ListUsersAdapter(usersArrayList)
//        getUserData()
}
  • Related