Ошибка создания пользовательского списка в фрагменте. Требуемая активность, найденный фрагмент - PullRequest
1 голос
/ 25 марта 2019

Я решил создать пользовательский список в моем фрагменте, но я получаю ошибку:

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
    }
}

Ответы [ 3 ]

0 голосов
/ 25 марта 2019

Используйте view.context вместо this

val myListAdapter = MyListAdapterInFragment(view.context,name,date,imgId)
0 голосов
/ 25 марта 2019

Класс фрагмента не расширяется Контекстный класс в его иерархии. В результате мы не можем передать его вместо context .

Однако вы можете ввести context внутрь фрагмента двумя способами.

  1. Используя родительский Activity , в котором находится фрагмент раздувается: набрав activity там, где требуется context.-> В вашем случае: val myListAdapter = MyListAdapterInFragment(activity,name,date,imgId)

  2. Использование context из переменной inflater, которую мы получаем в методе onCreateView(): набрав inflater.context-> В вашем случае: сделать свойство val mContext:Context.присвойте ему значение mContext = inflater.context в методе onCreateView ().Затем используйте его в объекте адаптера как val myListAdapter = MyListAdapterInFragment(mContext,name,date,imgId)

0 голосов
/ 25 марта 2019

Изменить это:

val myListAdapter = MyListAdapterInFragment(this,name,date,imgId)

К этому:

val myListAdapter = MyListAdapterInFragment(activity,name,date,imgId)

...