i want to insert menu gridview in tablayout with kotlin. i have been searching for many references in google but it wont help, still getting 1 error in adapter = FoodAdapter(this, foodsList)
. it says "Type missmatch : inferred type as fragmentHome but Context was expected". this is my code for fragmentHome.kt
package com.example.ako.nextbrilliantgeneration
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.content.Context
import android.support.v4.app.FragmentActivity
import kotlinx.android.synthetic.main.fragment_fragment_home.*
import kotlinx.android.synthetic.main.menu_entry.view.*
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"
class fragmentHome : Fragment() {
var adapter: FoodAdapter? = null
var foodsList = ArrayList<Menu>()
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_fragment_home, container, false)
foodsList.add(Menu("Coffee", R.drawable.profile))
foodsList.add(Menu("Espersso", R.drawable.profile))
foodsList.add(Menu("French Fires", R.drawable.profile))
foodsList.add(Menu("Honey",R.drawable.profile))
foodsList.add(Menu("Strawberry", R.drawable.profile))
foodsList.add(Menu("Sugar cubes", R.drawable.profile))
adapter = FoodAdapter(this, foodsList)
gvFoods.adapter = adapter
}
class FoodAdapter : BaseAdapter {
var foodsList = ArrayList<Menu>()
var context: Context? = null
constructor(context: Context, foodsList: ArrayList<Menu>) : super() {
this.context = context
this.foodsList = foodsList
}
override fun getCount(): Int {
return foodsList.size
}
override fun getItem(position: Int): Any {
return foodsList[position]
}
override fun getItemId(position: Int): Long {
return position.toLong()
}
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
val food = this.foodsList[position]
var inflator = context!!.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
var foodView = inflator.inflate(R.layout.menu_entry, null)
foodView.imgFood.setImageResource(food.image!!)
foodView.tvName.text = food.name!!
return foodView
}
}
}
is there any solution?
thanks
Your adapter takes in a non-null Context
, but you're passing in a Fragment
. You could try passing in your activity from your fragment, either:
adapter = FoodAdapter(activity!!, foodsList)
Or if you have the latest support library:
adapter = FoodAdapter(requireActivity(), foodsList)
You're getting unreachable error because you return from the method too early, simply move it to the bottom:
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
foodsList.add(Menu("Coffee", R.drawable.profile))
foodsList.add(Menu("Espersso", R.drawable.profile))
foodsList.add(Menu("French Fires", R.drawable.profile))
foodsList.add(Menu("Honey",R.drawable.profile))
foodsList.add(Menu("Strawberry", R.drawable.profile))
foodsList.add(Menu("Sugar cubes", R.drawable.profile))
adapter = FoodAdapter(this, foodsList)
gvFoods.adapter = adapter
// move this line to last
return inflater.inflate(R.layout.fragment_fragment_home, container, false)
}
The error is clear enough you try to pass this
which refer to Fragment object
but your adapter needs Context
then try to pass Activity
that extends from Context
.
Replace this line
adapter = FoodAdapter(this, foodsList)
With
adapter = FoodAdapter(getActivity(), foodsList)
Update
calls to Java get and set methods that can be replaced with the use of Kotlin synthetic properties.
getActivity() --> activity
This is a great update to help developers who are writing in Kotlin. The getActivity
method has been annotated with @Nullable
to better support Kotlin null-safety. Before, this method could return null and would therefore cause an IllegalStateException
in Kotlin code because a null object would be used where a non-null type was expected.
To best handle these cases for methods that have been annotated with @Nullable
, use the let
operator to unwrap the nullable into a non-null object.
getActivity()?.let { adapter = FoodAdapter(it, foodList) }
DO NOT use requireActivity()
to "ensure" a non-null activity because this method will throw an exception if your activity does happen to be null.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With