安卓中listview中没有数据时显示textview中的内容
时间: 2023-07-15 16:14:03 浏览: 141
在安卓中,如果ListView中没有数据,可以设置一个空布局来显示一些提示信息,比如“暂无数据”等。可以通过以下几个步骤来实现:
1. 在XML文件中定义ListView和空布局。例如:
```xml
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<TextView
android:id="@+id/emptyView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="暂无数据"
android:gravity="center"/>
```
2. 在Activity或Fragment中找到ListView,并设置其空布局。例如:
```java
ListView listView = findViewById(R.id.listView);
TextView emptyView = findViewById(R.id.emptyView);
listView.setEmptyView(emptyView);
```
这样,当ListView中没有数据时,就会显示空布局中的内容。注意,如果要在ListView中显示数据,需要先设置ListView的Adapter。
阅读全文
相关推荐















