Home > database >  Kotlin E/RecyclerView: No adapter attached; skipping layout
Kotlin E/RecyclerView: No adapter attached; skipping layout

Time:03-30

Hello everyone i have a problem with my news app. My app is perfectly working fine. I can get data from fire base and i can write them down while i am using debug. But when i am openin app in virtual device recycler view isn't showing in the fragment and giving this error : E/RecyclerView: No adapter attached; skipping layout

My adapter code is this : `class FeedAdapter(var userList :ArrayList<Post>) : RecyclerView.Adapter<FeedAdapter.MyViewHolder>() {

class MyViewHolder(itemView : View): RecyclerView.ViewHolder(itemView) {
    val comment : TextView = itemView.findViewById(R.id.comment)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {

    val itemView = LayoutInflater.from(parent.context).inflate(R.layout.user_item,parent,false)

    return MyViewHolder(itemView)
}

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

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

}`

My DashboardFragment is like this :

`class DashboardFragment : Fragment() {

private var postArrayList: ArrayList<Post> = arrayListOf()

private var db: FirebaseFirestore ? = null

private var recyclerView: RecyclerView? = null

private var feedAdapter: FeedAdapter? = null

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    db = FirebaseFirestore.getInstance()

    feedAdapter = FeedAdapter(arrayListOf())

    recyclerView?.layoutManager =
        LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)

    recyclerView?.adapter = feedAdapter

    eventChangeListener()


}

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

@SuppressLint("NotifyDataSetChanged")
private fun eventChangeListener() {

    db?.collection("Posts")?.addSnapshotListener { value, _ ->

        if (value != null) {
            if (!value.isEmpty) {
                val documents = value.documents
                for (document in documents) {
                    val comment: String = (document.get("comment") ?: "").toString()

                    val post = Post(comment)

                    Log.d("avs", post.toString())
                    postArrayList.add(post)
                }

                Log.d("avsArray", postArrayList.toString())

                feedAdapter?.userList = postArrayList

                feedAdapter?.notifyDataSetChanged()
            }
        }


    }


}

}`

i can't find the solution

I have tried to debug my app i wont have any problem with my DashboardFragment i can write data clearly. But when i am trying to debug my adapter class i can't get any thing from that

CodePudding user response:

  1. there is no setting of recyclerView var (use findViewById(R.id.yourOwnRecycler) or ViewBinding)
  2. all code accessing views have to be inside onViewCreated
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

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

    recyclerView?.layoutManager =
        LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)

    recyclerView?.adapter = feedAdapter

    eventChangeListener()
}
  • Related