为什么listview没有出现在xml中?

问题描述 投票:0回答:2

一切都已完成,即创建适配器,创建布局并传递数据但是当我将ListView添加到主活动时,则不会显示任何内容。我认为问题出在xml中。请帮忙

Activity2.xml

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.student.shopifysalespediasample.Activity2"
android:orientation="vertical"
android:id="@+id/list"
android:drawSelectorOnTop="true">
<!-- android:drawSelectorOnTop="true" shows a visual effect while clicking on listItem views-->

它显示空白屏幕,而不是在xml中显示示例列表项

android android-listview
2个回答
1
投票

工具:listitem |工具:listheader |工具:listfooter

这些属性适用于<AdapterView>(及其子类,如<ListView><RecyclerView>),用于指定应作为列表项,页眉或页脚在该适配器内绘制的布局。例如,我们的Contacts + app的fragment_contacts_xml布局声明了<RecyclerView>,这就是添加tools:listitem=”@layout/contact_item”之前和之后的样子

工具:ITEMCOUNT

此属性仅适用于<RecyclerView>,用于指定布局编辑器在布局预览中应呈现的列表项数。

默认情况下,Android Studio会显示<RecyclerView>的10个列表项。

因此,通常在添加tools:listitem属性后,<RecyclerView>将覆盖整个布局屏幕,您将无法再看到其下方的其他视图元素。在这种情况下,tools:itemCount属性将帮助您查看<RecyclerView>下方的元素。

您可以在hidden gems article找到更多。


-1
投票

您需要添加ArrayAdapter或CustomAdapter以及列表项布局以填充列表视图。如果在添加适配器问题后仍然存在则请分享完整代码。使用此链接作为参考。 https://www.vogella.com/tutorials/AndroidListView/article.html

© www.soinside.com 2019 - 2024. All rights reserved.