Reputation: 13
Hello everyone I have problem with code that I find in internet. Many times when I want to apply code from internet to my app I have problem with one thing "this"
there is a example
listview.adapter = Adapter(this, R.layout.tescik,list)
every time that I paste code to fragment I have red lined "this" and i really dont know how to fix it, I cant find the solution. Can anyone help me how to fix this (heh) problem?
there is is my code where i try to implement it.
package com.example.darwinaapp.podstrony
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ListView
import com.example.darwinaapp.Adapter
import com.example.darwinaapp.Model
import com.example.darwinaapp.R
import kotlinx.android.synthetic.main.fragment_promocje.*
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"
class Promocje : Fragment() {
private var param1: String? = null
private var param2: String? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
param1 = it.getString(ARG_PARAM1)
param2 = it.getString(ARG_PARAM2)
}
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
var listview = lista
var list = mutableListOf<Model>()
list.add(Model("Maravedi", "Bardzo dobre winko", R.drawable.heroesmerlot))
list.add(Model("Maravedi", "Bardzo dobre winko", R.drawable.heroesmerlot))
list.add(Model("Maravedi", "Bardzo dobre winko", R.drawable.heroesmerlot))
//There is my problem
listview.adapter = Adapter(this, R.layout.tescik,list)
//There is my problem
return inflater.inflate(R.layout.fragment_promocje, container, false)
}
companion object {
fun newInstance(param1: String, param2: String) =
Promocje().apply {
arguments = Bundle().apply {
putString(ARG_PARAM1, param1)
putString(ARG_PARAM2, param2)
}
}
}
}
Upvotes: 1
Views: 367
Reputation: 2028
you do not have to pass the context to the adapter, because you can get context from the view group of createViewHolder(parent:ViewGroup, viewType:Int)
and in onBindViewHolder, you can get context from itemView.context
Upvotes: 1
Reputation: 75788
You can use there requireContext()
.
listview.adapter = Adapter(requireContext(), R.layout.tescik,list)
Upvotes: 2