14

I have a fragment with RecyclerAdapter inside it. I want to initialize the adapter in the onCreateView method but it throws the error of "Type mismatch. Required : Context , Found : FragmentActivity" in this statement

I have no idea why the first one showed this error and the second one did not contains compile time error.

Error shown

recyclerView!!.adapter = RestaurantMenuAdapter(activity)

No Error shown

recyclerView!!.layoutManager = LinearLayoutManager(activity)

Fragment.kt

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                          savedInstanceState: Bundle?): View? {
    // Inflate the layout for this fragment
    val view = inflater.inflate(R.layout.fragment_restaurant_menu, container, false)
    recyclerView = view.findViewById(R.id.restaurant_container)
    recyclerView!!.adapter = RestaurantMenuAdapter(activity)
    recyclerView!!.layoutManager = LinearLayoutManager(activity)

RecyclerAdapter.kt

class RestaurantMenuAdapter  (val context : Context) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val inflater = parent.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        return object : RecyclerView.ViewHolder(inflater.inflate(R.layout.item_menu1, parent, false)) {

        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {

    }

    override fun getItemCount(): Int {
        return 10
    }
}
Long Ranger
  • 4,766
  • 7
  • 38
  • 65
  • why you pass context to your adapter? i think you do not need to pass it. – savepopulation Jan 10 '18 at 08:05
  • I may use context in the future. In fact, I used to pass Context as parameter in the adapter in java as the same way I did it in the above. In the past, I used to pass Fragment getActivity() as Context in the Adapter constructor as the parameter which accepted Context Type. I have no idea why this time did not work – Long Ranger Jan 10 '18 at 08:12
  • @LongRanger Solved this yet? – IntelliJ Amiya Jan 10 '18 at 08:47
  • Yes, at least I accepted to use activity.applicationContext to solve my problem. But I am still confusing with the activity in fragment – Long Ranger Jan 10 '18 at 08:53

4 Answers4

16

Change this to-:

recyclerView!!.adapter = RestaurantMenuAdapter(activity)

To-:

recyclerView!!.adapter = RestaurantMenuAdapter(activity.applicationContext)
Yogesh Paliyal
  • 604
  • 7
  • 21
Shivam Oberoi
  • 1,374
  • 1
  • 6
  • 15
6
recyclerView!!.adapter = RestaurantMenuAdapter(this)

To

recyclerView!!.adapter = RestaurantMenuAdapter(this.requireActivity())
MartenCatcher
  • 2,209
  • 7
  • 22
  • 31
Shivam Tripathi
  • 441
  • 5
  • 6
2

Keep the adapter as it and just use "activity!!" where you are initializing adapter.

recyclerView.adapter = RestaurantMenuAdapter(activity!!)

Your adapter will remain same.

class RestaurantMenuAdapter  (val context : Context) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
    val inflater = parent.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
    return object : RecyclerView.ViewHolder(inflater.inflate(R.layout.item_menu1, parent, false)) {

    }
}

override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {

}

override fun getItemCount(): Int {
    return 10
}
}
Nauman Ash
  • 887
  • 1
  • 8
  • 18
0

Change in Recycler Adapter from Context to Activity.

class RestaurantMenuAdapter  (val context : Activity) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val inflater = parent.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        return object : RecyclerView.ViewHolder(inflater.inflate(R.layout.item_menu1, parent, false)) {

        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {

    }

    override fun getItemCount(): Int {
        return 10
    }
}
Yogesh Paliyal
  • 604
  • 7
  • 21