gpt4 book ai didi

xml - 片段错误Kotlin中的ListView

转载 作者:行者123 更新时间:2023-12-02 13:06:37 24 4
gpt4 key购买 nike

KT的新手。我在执行列表 View 时遇到一些错误。不确定为什么我会遇到这些问题。是因为它扩展了片段吗?有人可以指出我正确的方向吗?

import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ListView
import android.widget.ArrayAdapter

class Chatroom : Fragment() {

private lateinit var listView ListView

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {

var listView = findViewById(R.id.chatroom_list_view) as ListView

val testList = "hu, tu, su, send, pe, t"

val adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, testList)
listView.adapter = adapter


// Inflate the layout for this fragment
return inflater.inflate(R.layout.chatroom, container, false)
}

失误

Picture of Errors

.xml
<ListView
android:id="@+id/chatroom_list_view"
android:layout_width="368dp"
android:layout_height="436dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toTopOf="@+id/messageTextField"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/search" />

最佳答案

像这样更改您的代码。

class Chatroom : Fragment() {

private lateinit var listView: ListView

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
val rootView = inflater.inflate(R.layout.chatroom, container, false)

listView = rootView.findViewById(R.id.chatroom_list_view)

val testList = arrayOf("hu", "tu", "su", "send", "pe", "t")

val adapter = ArrayAdapter(context, android.R.layout.simple_list_item_1, testList)
listView.adapter = adapter


return rootView
}

关于xml - 片段错误Kotlin中的ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50280152/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com