android-fragments ListView数组适配器错误“无法使用提供的参数调用以下任何函数,”

6qfn3psc  于 2022-11-14  发布在  Android
关注(0)|答案(1)|浏览(127)

我正在尝试使用ID=lv的ListView创建一个片段,但ArrayAdapter似乎无法工作
这是片段代码

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

z2acfund

z2acfund1#

您需要传递上下文而不是this

val adapter = ArrayAdapter(requireContext(), R.layout.listview_item, array)

相关问题