use*_*624 16 android android-listview empty-list
<TextView
android:id="@android:id/empty"
style="@style/FacebookFont"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="@string/no_result_found"
android:textColor="@color/white" />
Run Code Online (Sandbox Code Playgroud)
这是空视图的xml代码.我将空视图设置如下:
list.setAdapter(adapter);
list.setEmptyView(findViewById(android.R.id.empty));
Run Code Online (Sandbox Code Playgroud)
即使在填充listview之前,我也会在列表视图中显示这些项目,并显示空视图.当listview包含某个项目时,我不希望显示该空视图.
请帮忙.
Dha*_*mar 14
如果您正在使用,ListActivity则无需手动设置空视图.list.setEmptyView(findViewById(android.R.id.empty));从您的代码中删除.
如下所示更改您的布局然后EmptyView如果列表没有项目则自动添加:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="8dp"
android:paddingRight="8dp">
<ListView android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00FF00"
android:layout_weight="1"
android:drawSelectorOnTop="false"/>
<TextView android:id="@android:id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0000"
android:text="No data"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
Ven*_*noM 11
如果您MainActivity没有扩展,则必须在设置适配器之前设置空视图ListActivity.
试试这个
list.setEmptyView(findViewById(android.R.id.empty));
list.setAdapter(adapter);
Run Code Online (Sandbox Code Playgroud)
抱歉回复晚了!
替代方法:使您的活动从ListActivity扩展.并textview使用id 添加到列表布局中android.R.id.empty
试试这个
View view = getLayoutInflater() .inflate(<resource id of the empty view>, null);
ViewGroup viewGroup= ( ViewGroup)list.getParent();
viewGroup.addView(view);
list.setEmptyView(view);
list.setAdapter(adapter);
Run Code Online (Sandbox Code Playgroud)
有一些正确的答案,但是,我认为这是我发现的最佳方法:
View emptyView = getLayoutInflater().inflate(R.layout.empty_list_cart, null);
addContentView(emptyView, listView.getLayoutParams()); // You're using the same params as listView
listView.setEmptyView(emptyView);
listView.setAdapter(adapter);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
40054 次 |
| 最近记录: |