I'm trying to create a fragment with a ListView with the id=lv and the ArrayAdapter doesn't seem to work
this is the fragment code
class Istoric : Fragment() {
var array = arrayOf("item1","item2")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val adapter = ArrayAdapter(this,R.layout.listview_item,array)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_istoric, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val lv1: ListView = view.findViewById(R.id.lv)
lv1.setAdapter(adapter)
}
}
CodePudding user response:
You need to pass context instead of this
val adapter = ArrayAdapter(requireContext(), R.layout.listview_item, array)