简体   繁体   中英

recyclerview in fragment context not working in kotlin

Im creating a really basic recyclerview in fragment_home.xml which is linked to a <FrameLayout /> inserted in activity_main.xml

It works well...until I create the HomeFragment class in HomeFragment.kt , which inflates fragment_home.xml

Then I want to add up the arrayList ( arrayHomeMenu ) in the function onActivityCreated()

my problem is with this line

  val homeMenuAdapter = HomeMenuAdapter(arrayHomeMenu, this) //context "this" appears with red underline

It retrieves error, so I cant continue...

HomeFragment.kt

   class HomeFragment : Fragment() {
   override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
    ): View?{
    return inflater.inflate(R.layout.fragment_home, container, false)

 }


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


    //create list of words
    val arrayHomeMenu = ArrayList<HomeMenuModel>()

    arrayHomeMenu.add(HomeMenuModel("Verbs","List of 461  words", R.drawable.ic_logo))
    arrayHomeMenu.add(HomeMenuModel("Nouns","List of 52 words", R.drawable.ic_logo))
    arrayHomeMenu.add(HomeMenuModel("Adjectives","List of 65 words", R.drawable.ic_logo))
    arrayHomeMenu.add(HomeMenuModel("Adverbs","List of 345 words", R.drawable.ic_logo))

    val homeMenuAdapter = HomeMenuAdapter(arrayHomeMenu, this) //context "this" appears with red underline


    homeMenu_recyclerView.layoutManager = LinearLayoutManager(context)
    homeMenu_recyclerView.adapter = homeMenuAdapter

}

This is a context problem....

I tried to replace this by activity , context , Context , applicationContext with not success at all...

HomeMenuAdapter.kt

class HomeMenuAdapter(val arrayList: ArrayList<HomeMenuModel>, val context: Context) :
RecyclerView.Adapter<HomeMenuAdapter.ViewHolder>() {

    ....//correct content
   }

It works correctly when I create the recyclerview direct in activity_main.xml and work directly with MainActivity.kt

Once I move the recyclerview to a fragment...it throws the error described above.

HomeMenuModel.kt

  class HomeMenuModel(val hm_title:String,val hm_description: String, val hm_image:Int)

I was checking this answer , but no success...

what am doing wrong?? thank you

The reason why you can use this as context inside an Activity, is because Activity extends Context.

Fragment however does not extend Context so you cannot use this .

The reason why activity , context also don't work is Kotlin's distinction of nullable types. While activity and context do return a Context, the return value is nullable. You can see this by paying close attention to the error message that appears when hovering over the red underline:

Type mismatch.
Required:
Context
Found:
Context? (or FragmentActivity?)

The question mark indicates that this is a nullable type, while a non-nullable Context is required. The reason why they're nullable is that the Fragment can only retrieve the Activity when it is attached to it, which is not always the case.

However, Fragment has a convenient method called requireContext() to work around this issue. It has a non-nullable return type but will instead throw an exception if it cannot retrieve the context, so it is on you to make sure to only call it when the Fragment is attached.

In short, you should be able to instantiate your adapter like this:

val homeMenuAdapter = HomeMenuAdapter(arrayHomeMenu, requireContext())

Since you are in a Fragment this can't be passed as a context:

val homeMenuAdapter = HomeMenuAdapter(arrayHomeMenu, this)

In the above code you are trying to pass fragment as a context, so instead what you can do is get the context of the fragment. So you can do:

val homeMenuAdapter = HomeMenuAdapter(arrayHomeMenu, requireContext())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM