skip to Main Content

I’m learning Kotlin in Adroid Studio and recently I have stuck on RecyclerView.

The problem is that I’m not able to go forward due to error showing on LinearLayoutManager(this).

enter image description here

My little code looks like:

    package com.store.example
    
    import android.os.Bundle
    import androidx.fragment.app.Fragment
    import android.view.LayoutInflater
    import androidx.recyclerview.widget.LinearLayoutManager
    import kotlinx.android.synthetic.main.fragment_store.*
    
    class StoreFragment : Fragment() {
    
        override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
            super.onViewCreated(view, savedInstanceState)
    
            // call recyclerView this id (fragment_store)
            recyclerView.layoutManager = LinearLayoutManager(this)
        }
    
        override fun onCreateView(
            inflater: LayoutInflater, container: ViewGroup?,
            savedInstanceState: Bundle?
        ): View? {
            // Inflate the layout for this fragment
            return inflater.inflate(R.layout.fragment_store, container, false)
        }
    }

My 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=".StoreFragment">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:listitem="@layout/card_view_store" />

</FrameLayout>

LinearLayoutManager(this) is underlined and tells me that:

Type mismatch: inferred type is StoreFragment but Context! was expected

I’m not a advanced programist, rather a newbie and I’ve been trying to fix this remark but at this moment its beyond my skills.

Thank you for your help.

4

Answers


  1. Try this instead,

    recyclerView.layoutManager = LinearLayoutManager(activity)
    

    or

    recyclerView.layoutManager = LinearLayoutManager(requireContext())
    

    activity returns fragment`s host activity whereas requireContext() returns the context of the fragment`s hosting activity.

    Login or Signup to reply.
  2. this refers to the Fragment (StoreFragment to be precise), but the constructor for LinearLayoutManager needs a reference to an instance of Context.

    You have a number of different ways to get a reference to a Context.

    • this.context will return the context property of the fragment. Can omit this. and just use context to do the same thing.
    • view.context will return the context property of the view argument.
    • requireActivity() will return the activity that controls the fragment which can also be used as an instance of Context.
        override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
            super.onViewCreated(view, savedInstanceState)
        
             // call recyclerView this id (fragment_store)
            recyclerView.layoutManager = LinearLayoutManager(this.context)
        }
    
    Login or Signup to reply.
  3. Use requireContext() you can get the fragment’s hosting activity.

    So you can try this:

    recyclerView.layoutManager = LinearLayoutManager(requireContext())
    
    Login or Signup to reply.
  4. Just to add to the answers you’ve been given, Context parameters show up a lot in Android, so you need to know how to be able to get one. An Activity is a Context, which is why if your code were in an Activity, you could pass this.

    But you’re in a Fragment, which isn’t a subclass of Context, so this won’t work! But Fragments do have access to a Context – either through getContext (or in Kotlin, the context property equivalent) or requireContext.

    getContext returns null if you don’t have one yet, meaning you have to handle that nullability. requireContext throws an exception if you don’t have access to one, so it doesn’t need to be nullable, and this is the one you’re supposed to use. So long as you call requireContext in onCreate or later, the Fragment will have been attached to its context and it’s safe to use.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search