Home > other >  I am trying to show recyclerView in Fragment but I am getting error
I am trying to show recyclerView in Fragment but I am getting error

Time:12-20

class FeedRecyclerAdapter (private val postList : ArrayList<Post>) : RecyclerView.Adapter<FeedRecyclerAdapter.PostHolder>() {

class PostHolder(val binding: FragmentDataBinding) : RecyclerView.ViewHolder(binding.root)

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PostHolder {
        val binding = FragmentDataBinding.inflate(LayoutInflater.from(parent.context),parent,false)
        return PostHolder(binding)
}

override fun onBindViewHolder(holder: PostHolder, position: Int) {

    holder.binding.verimText.text = postList.get(position).lsi

}

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

Here are the codes written for recyclerView.

private lateinit var postArrayList : ArrayList<Post>



override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    auth = Firebase.auth
    firestore = Firebase.firestore

    postArrayList = ArrayList<Post>()

    getData()

}



override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View {
    _binding = FragmentDataBinding.inflate(inflater, container, false)
    val view = binding.root
    val recyclerView = view.findViewById(R.id.recyclerView) as RecyclerView
    recyclerView.layoutManager = LinearLayoutManager(activity)
    recyclerView.adapter = FeedRecyclerAdapter(postArrayList)
    return view

}

private fun getData(){
    firestore.collection("Posts").addSnapshotListener { value, error ->

        if (error!=null){
            Toast.makeText(requireContext(),error.localizedMessage,Toast.LENGTH_SHORT).show()
        }else{
            if (value !=null){
                if (!value.isEmpty){
                    val documents = value.documents
                    for (document in documents){
                        val araziBoyutu = document.get("Arazi Boyutu") as String
                        val araziEgimi = document.get("Arazi Eğimi") as String
                        val panelBoyutu = document.get("Panel Boyutu") as String
                        val panelSayisi = document.get("Panel Sayisi") as String
                        val sehir = document.get("Şehir") as String


                        val post = Post(panelSayisi,panelBoyutu,araziEgimi,araziBoyutu,sehir)
                        postArrayList.add(post)
                    }
                }
            }
        }
    }
}

The codes here are the part where I define the RecylcerView and save the information.

I can pull the data, I can see it on firebase, when I print it with println, I can read it in the console, I can go to the page where the text should be written, but I can't see this data in the verimText TextView I'm trying to print.

CodePudding user response:

Try to call notifyDataSetChanged() or notifyItemRangeInserted() after adding post.

CodePudding user response:

Recyclerview should be declared inside onViewCreated Method

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
            super.onViewCreated(view, savedInstanceState)
       val recyclerView = view.findViewById(R.id.recyclerView) as RecyclerView
       recyclerView.layoutManager = LinearLayoutManager(activity)
       recyclerView.adapter = FeedRecyclerAdapter(postArrayList)
    }

CodePudding user response:

move this line

recyclerView.adapter = FeedRecyclerAdapter(postArrayList)

to

val post = Post(panelSayisi,panelBoyutu,araziEgimi,araziBoyutu,sehir)
postArrayList.add(post)
recyclerView.adapter = FeedRecyclerAdapter(postArrayList)//move to here
  • Related