0

I am trying to show this datas to my recycler view image of my firebase rtdb but they are not showed in the recycler view.

My Codes are below fragment_group_notice.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/GroupNoticeFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".groupTab.groupNotices.GroupNoticesFragment">



        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/groupNoticeListRecycler"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager">

        </androidx.recyclerview.widget.RecyclerView>


    
</FrameLayout>

card_group_notice.xml

<?xml version="1.0" encoding="utf-8"?>
<layout
    xmlns:android="http://schemas.android.com/apk/res/android">
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:background="@color/primary_green"
        android:orientation="horizontal"
        android:padding="10dp">
        
        <TextView
            android:id="@+id/NoticeNameCard"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Notice Name"
            android:textColor="@color/pure_white"
            android:textSize="25sp" />

    </LinearLayout>

</layout>

GroupNoticesFragment

class GroupNoticesFragment : Fragment() {
    private var _binding: FragmentGroupNoticesBinding? = null
    private val binding get() = _binding!!
    private lateinit var recycler: RecyclerView
    private  var adapter: NoticeAdapter?= null
    private lateinit var viewModel: NoticeViewModel

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        container?.removeAllViews()
        _binding = FragmentGroupNoticesBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        recycler = binding.groupNoticeListRecycler
        recycler.layoutManager = LinearLayoutManager(context)
        recycler.setHasFixedSize(true)
        adapter = NoticeAdapter()
        recycler.adapter = adapter
        viewModel = ViewModelProvider(this)[NoticeViewModel::class.java]

        viewModel.allNotices.observe(viewLifecycleOwner) {
            adapter!!.updateNoticeList(it)
        }

    }


}

NoticeAdapter.kt

class NoticeAdapter : RecyclerView.Adapter<NoticeAdapter.NoticeViewHolder>() {
    private val notices = ArrayList<GroupNoticeData>()

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

    override fun onBindViewHolder(holder: NoticeViewHolder, position: Int) {
        val currentNotice = notices[position]
        holder.noticeName.text = currentNotice.taskName
    }


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

    fun updateNoticeList(notice: List<GroupNoticeData>) {

        this.notices.clear()
        this.notices.addAll(notice)
        notifyDataSetChanged()

    }

    class NoticeViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val noticeName: TextView = itemView.findViewById(R.id.NoticeNameCard)

    }
}

GroupNoticeData

data class GroupNoticeData(
    val userId: String,
    val taskName: String,
    val taskDescription: String,
    val taskDate: String,
    val path: String?,
    val groupId: String
) {
    fun toMap(): Map<String, Any?> {
        return mapOf(
            "userId" to userId,
            "taskName" to taskName,
            "taskDescription" to taskDescription,
            "taskDate" to taskDate,
            "path" to path,
            "groupId" to groupId
        )
    }
}

NoticeViewModel

class NoticeViewModel : ViewModel() {

    private val repository : NoticeRepo = NoticeRepo().getInstance()
    private val _allNotices = MutableLiveData<List<GroupNoticeData>>()
    val allNotices : LiveData<List<GroupNoticeData>> = _allNotices

    init {
        repository.loadNotices(_allNotices)
    }
}

NoticeRepo

class NoticeRepo{
    val auth = Firebase.auth
    val user = auth.currentUser!!.uid
    private val noticeReference : DatabaseReference = FirebaseDatabase.getInstance().getReference("groupNotice").child(user)
    @Volatile private var INSTANCE : NoticeRepo?= null

    fun getInstance(): NoticeRepo {
        return INSTANCE ?: synchronized(this) {

            val instance = NoticeRepo()
            INSTANCE = instance
            instance
        }
    }

    fun loadNotices(allNoticeList: MutableLiveData<List<GroupNoticeData>>) {

        noticeReference.addValueEventListener(object : ValueEventListener {
            override fun onDataChange(snapshot: DataSnapshot) {

                try {

                    val noticeList : List<GroupNoticeData> = snapshot.children.map { dataSnapshot ->

                        dataSnapshot.getValue(GroupNoticeData::class.java)!!

                    }
                    allNoticeList.postValue(noticeList)

                }catch (_: Exception){

                }

            }

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


        })
    }
}

I tried to change the reference in repo class aslo checked the items height width issue but couldn't come to any solution

There isn't any error but the code is supposed to load data from the rtdb reference but it isn't showing anything. The recycler view was supposed to fill up using the card_group_notice.xml where the information were supposed to come from the database child as you can see groupNotice/(userId)

4
  • "items Not showing" doesn't provide enough information so we can help. What exactly in this code doesn't work the way you expect? Tell us what is wrong with shared code. Do you have any errors?
    – Alex Mamo
    Commented Nov 25, 2022 at 11:10
  • There isn't any error but the code is supposed to load data from the rtdb reference but it isn't showing anything. The recycler view was supposed to fill up using the card_group_notice.xml where the informations were supposed to come from the database child as you can see groupNotice/(userId) Commented Nov 25, 2022 at 12:15
  • First of all, stop ignoring errors. At a minimum, please add Log.d(TAG, error.getMessage());. Do you get something printed out in the logcat?
    – Alex Mamo
    Commented Nov 25, 2022 at 13:13
  • Well as I just getting started I am not familiar with using logcat! I will try to implement this. Also I have solved it but not sure how it was solved :) I just wrote them from the beginning and now the work is done! Thank for your concern I will surely use Logcat in upcoming days! Commented Nov 26, 2022 at 14:05

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.