Я решил создать пользовательский список в моем фрагменте, но я получаю ошибку:
FragmentOne.kt: (41, 53): Несоответствие типов: предполагаемый тип - FragmentOne.
но активность была ожидаемой.
Код ниже.
FragmentOne.kt
class FragmentOne : Fragment() {
val name = arrayOf(
"First catch","Second catch","Third catch","Fourth catch"
)
val date = arrayOf(
"01.01.2019", "02.02.2010", "03.03.2003", "04.04.2004"
)
val imgId = arrayOf(
R.drawable.fish
)
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.screen1, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val myListAdapter = MyListAdapterInFragment(this,name,date,imgId)
ListViewInFragment.adapter = myListAdapter
}
}
MyListAdapterInFragment.kt
class MyListAdapterInFragment(private val context: Activity, private val title: Array<String>, private val date: Array<String>, private val imgid: Array<Int>)
: ArrayAdapter<String>(context, R.layout.list_iteminfragment, title) {
override fun getView(position: Int, view: View?, parent: ViewGroup): View {
val inflater = context.layoutInflater
val rowView = inflater.inflate(R.layout.list_iteminfragment, null, true)
val titleText = rowView.findViewById(R.id.title) as TextView
val imageView = rowView.findViewById(R.id.imageViewInFragment) as ImageView
val subtitleText = rowView.findViewById(R.id.date) as TextView
titleText.text = title[position]
imageView.setImageResource(imgid[position])
subtitleText.text = date[position]
return rowView
}
}