'E/RecyclerView: No adapter attached; skipping layout . Adapter doesn't want to be recognized/detected

I'm really sorry for asking the same question like 100 other people did but i have tried every solution provided and I can't seem to find what is wrong with my code.

Currently the problem with the code is that the recyclerview doesn't want to recognize my adapter. I have tried changing the View around and tried some solution that have been provided by other thread and none of them have worked so far.

Have tried debugging with populating the list with dummies but it's also not the problem either

Adapter

class mkAdapter(val mkList: ArrayList<mk>): RecyclerView.Adapter<mkAdapter.mkViewHolder>() {
class mkViewHolder(val v:View): RecyclerView.ViewHolder(v){

}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): mkViewHolder {
    val inflater = LayoutInflater.from(parent.context)
    val v = inflater.inflate(R.layout.activity_card, parent, false)
    return mkViewHolder(v)
}

override fun onBindViewHolder(holder: mkViewHolder, position: Int) {
    holder.v.txtKode.text = mkList[position].kode
    holder.v.txtSKS.text = mkList[position].sks.toString()
    holder.v.txtMK.text = mkList[position].nama
}

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

}

Fragment

class FragmentMK : Fragment() {
private var param1: String? = null
private var param2: String? = null

var mklists: ArrayList<mk> = ArrayList()
var v:View? = null


override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    arguments?.let {
        param1 = it.getString(ARG_PARAM1)
        param2 = it.getString(ARG_PARAM2)
    }

    mklists.add(mk("test","test",1))
    mklists.add(mk("test","test",1))
    mklists.add(mk("test","test",1))
    updateList()

}

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {

    v = inflater.inflate(R.layout.fragment_mk,container,false)
    return v
}

fun updateList(){
    val lm:LinearLayoutManager = LinearLayoutManager(activity)
    var recyclerView = v?.findViewById<RecyclerView>(R.id.recyclerview)
    recyclerView?.layoutManager = lm
    recyclerView?.setHasFixedSize(true)
    recyclerView?.adapter = mkAdapter(mklists)
}


Solution 1:[1]

The problem is you're trying to create the list before view inflation in other words before the view is even created. Solution is to override onViewCreated method then you need to call updateList() inside onViewCreated.

    override fun onViewCreated(view : View, savedInstanceState : Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        updateList()
    }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1