【发布时间】:2020-04-11 22:10:52
【问题描述】:
我无法在 Fragment 中实现列表视图。我只有一个 ListView 和 ArrayList。我的 ArrayList 是一个字符串,需要 5 个名称。
class GroupFragment : Fragment() {
companion object {
fun newInstance(): GroupFragment {
return GroupFragment() } }
var arrayList= ArrayList<String>()
private lateinit var Adapter: ArrayAdapter<String>
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
val view: View = inflater.inflate(R.layout.fragment_group, container, false)
val activity = activity as Context
val listview = view.findViewById<ListView>(R.id.list_view)
arrayList.add("Ajay")
arrayList.add("Vijay")
arrayList.add("Prakash")
arrayList.add("Rohan")
arrayList.add("Vijay")
Adapter = ArrayAdapter(activity, android.R.layout.simple_list_item_1,arrayList )
listview.adapter = ChatAdapter(activity, android.R.layout.simple_list_item_1, arrayList)
return view
}
class ChatAdapter(var mCtx: Context, var resources:Int, var items:List<String>):ArrayAdapter<String>(mCtx, resources, items) {
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
val layoutInflater:LayoutInflater = LayoutInflater.from(mCtx)
val view:View = layoutInflater.inflate(resources, null)
val titleTextView: TextView = view.findViewById(R.id.textView)
var mItem: String = items[position]
titleTextView.text = mItem
return super.getView(position, convertView, parent)
}
}
}
我的代码如上,但它没有返回任何内容。没看懂为什么??是“适配器”的问题吗? 上下文有什么问题吗??
【问题讨论】:
-
这里没有足够的信息来确定问题所在。请edit您的问题提供minimal reproducible example来证明问题。
-
你确定你的数组不是空的吗?
-
我再次编辑。你能检查一下吗?问题出在哪里? @MikeM。
-
不,它不是空的。 @ImanX
-
您根本没有使用
ArrayAdapter,因此您可以删除该行。不过,除此之外,我们仍然需要更多信息;至少ChatAdapter类,可能还有更多。汇总minimal reproducible example 后,您将确切知道要在问题中包含哪些内容。
标签: android-studio android-fragments kotlin android-arrayadapter