ListView上的setEmptyView不起作用

Wij*_*den 3 android listview

我正在尝试在listView为空时设置空视图.这是我的完整XML布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/fondgris"
    android:orientation="vertical" >

    <!-- Header  Starts -->

    <RelativeLayout
        android:id="@+id/head"
        android:layout_width="fill_parent"
        android:layout_height="35sp"
        android:background="@layout/header_gradient"
        android:paddingBottom="5dip"
        android:paddingTop="5dip" >

        <TextView
            android:id="@+id/activity"
            android:layout_width="wrap_content"
            android:layout_height="35dp"
            android:layout_alignParentLeft="true"
            android:layout_centerInParent="true"
            android:layout_marginLeft="5dp"
            android:gravity="center_vertical"
            android:scaleType="center"
            android:text="List of projects"
            android:textColor="#ffffff"
            android:textSize="13sp"
            android:textStyle="bold"
            android:tint="#0E5EC7" />

        <ImageButton
            android:id="@+id/logout"
            android:layout_width="30dp"
            android:layout_height="35dp "
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="0dp"
            android:background="@android:color/transparent"
            android:onClick="logout"
            android:scaleType="center"
            android:src="@drawable/sortie"
            android:tint="#FFFFFF" >
        </ImageButton>

        <ImageButton
            android:id="@+id/home"
            android:layout_width="35dp"
            android:layout_height="35dp "
            android:layout_centerVertical="true"
            android:layout_toLeftOf="@id/logout"
            android:background="@android:color/transparent"
            android:onClick="home"
            android:scaleType="center"
            android:src="@drawable/home" >
        </ImageButton>

        <ImageButton
            android:id="@+id/back"
            android:layout_width="wrap_content"
            android:layout_height="35dp "
            android:layout_centerVertical="true"
            android:layout_toLeftOf="@id/home"
            android:background="@android:color/transparent"
            android:onClick="back"
            android:scaleType="center"
            android:src="@drawable/back"
            android:tint="#FFFFFF" >
        </ImageButton>
    </RelativeLayout>
    <!-- Editext and ImageView for Search -->

    <RelativeLayout
        android:id="@+id/linearLayout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <EditText
            android:id="@+id/inputSearch"
            android:layout_width="150dp"
            android:layout_height="35dp"
            android:layout_centerVertical="true"
            android:layout_toLeftOf="@+id/bnSearch"
            android:hint="Search..."
            android:inputType="textVisiblePassword"
            android:textSize="12sp" />

        <ImageButton
            android:id="@+id/bnSearch"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/inputSearch"
            android:layout_centerInParent="true"
            android:layout_toLeftOf="@+id/bnClear"
            android:background="@android:color/transparent"
            android:src="@drawable/search" />

        <ImageButton
            android:id="@+id/bnClear"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/inputSearch"
            android:layout_centerInParent="true"
            android:layout_marginRight="3dip"
            android:background="@android:color/transparent"
            android:onClick="ClearSearch"
            android:src="@drawable/cancel" />

        <Button
            android:id="@+id/imageButtonAddProject"
            android:layout_width="wrap_content"
            android:layout_height="35dp"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_centerInParent="true"
            android:drawableLeft="@drawable/add"
            android:onClick="addProject"
            android:text=" Add Project"
            android:textColor="@color/text_color"
            android:textSize="12sp" />
    </RelativeLayout>

    <!-- List View -->

    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="337dp"
        android:layout_weight="0.74" >
    </ListView>

    <TextView
        android:id="@+id/emptyList"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:text="Liste vide "
        android:textSize="12sp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.04"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/bnPrevious"
            android:layout_width="80dp"
            android:layout_height="35dp"
            android:layout_marginLeft="30dp"
            android:background="@android:color/transparent"
            android:text="&lt;&lt; Prev"
            android:textSize="12sp" />

        <TextView
            android:id="@+id/page"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Page"
            android:textSize="12sp" />

        <EditText
            android:id="@+id/editpage"
            android:layout_width="wrap_content"
            android:layout_height="35dp"
            android:textSize="12sp" />

        <TextView
            android:id="@+id/of"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="of "
            android:textSize="12sp" />

        <TextView
            android:id="@+id/nbpage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="12sp" />

        <Button
            android:id="@+id/bnNext"
            android:layout_width="80dp"
            android:layout_height="35dp"
            android:layout_marginRight="30dp"
            android:background="@android:color/transparent"
            android:text="Next >>"
            android:textSize="12sp" />
    </LinearLayout>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

它没有任何效果,我做了调试,如果没有将列表设置为空,它进入部分,列表继续出现!我不明白为什么,我感谢你的帮助.

final ProjectContainer container = gson.fromJson(resultat,
                    ProjectContainer.class);
            final ListView lv = (ListView) findViewById(R.id.list);
            TextView emptyList = (TextView) findViewById(R.id.emptyList);
            // if list empty
            if (container == null) {
                lv.setEmptyView(emptyList);
                emptyList.setText("No Projects Found");

            }
Run Code Online (Sandbox Code Playgroud)

提前致谢.

Adn*_*nan 23

  1. 使用RelativeLayout

  2. 首先添加emptyList视图

  3. 添加listView(emptyList必须在listview后面)

  4. 如果从json获得0项,请将listView的可见性设置为View.INVISIBLE.

编辑:

好的,你需要稍微改变你的XML.

将emptyList和list括在RelativeLayout中,如下所示.

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    >

    <TextView
        android:id="@+id/emptyList"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="There is no item"
        android:textSize="25sp" />

    <ListView
        android:id="@+id/list" android:background="@android:color/white"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
    </ListView>

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

要显示空文本,请将listView可见性设置为GONE.这是我身边的示例输出.首先是listView可见性可见,第二个是listview可见性GONE

这里有完整更新的xml代码.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/fondgris"
    android:orientation="vertical" >

    <!-- Header  Starts -->

    <RelativeLayout
        android:id="@+id/head"
        android:layout_width="fill_parent"
        android:layout_height="35sp"
        android:background="@layout/header_gradient"
        android:paddingBottom="5dip"
        android:paddingTop="5dip" >

        <TextView
            android:id="@+id/activity"
            android:layout_width="wrap_content"
            android:layout_height="35dp"
            android:layout_alignParentLeft="true"
            android:layout_centerInParent="true"
            android:layout_marginLeft="5dp"
            android:gravity="center_vertical"
            android:scaleType="center"
            android:text="List of projects"
            android:textColor="#ffffff"
            android:textSize="13sp"
            android:textStyle="bold"
            android:tint="#0E5EC7" />

        <ImageButton
            android:id="@+id/logout"
            android:layout_width="30dp"
            android:layout_height="35dp "
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="0dp"
            android:background="@android:color/transparent"
            android:onClick="logout"
            android:scaleType="center"
            android:src="@drawable/sortie"
            android:tint="#FFFFFF" >
        </ImageButton>

        <ImageButton
            android:id="@+id/home"
            android:layout_width="35dp"
            android:layout_height="35dp "
            android:layout_centerVertical="true"
            android:layout_toLeftOf="@id/logout"
            android:background="@android:color/transparent"
            android:onClick="home"
            android:scaleType="center"
            android:src="@drawable/home" >
        </ImageButton>

        <ImageButton
            android:id="@+id/back"
            android:layout_width="wrap_content"
            android:layout_height="35dp "
            android:layout_centerVertical="true"
            android:layout_toLeftOf="@id/home"
            android:background="@android:color/transparent"
            android:onClick="back"
            android:scaleType="center"
            android:src="@drawable/back"
            android:tint="#FFFFFF" >
        </ImageButton>
    </RelativeLayout>
    <!-- Editext and ImageView for Search -->

    <RelativeLayout
        android:id="@+id/linearLayout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <EditText
            android:id="@+id/inputSearch"
            android:layout_width="150dp"
            android:layout_height="35dp"
            android:layout_centerVertical="true"
            android:layout_toLeftOf="@+id/bnSearch"
            android:hint="Search..."
            android:inputType="textVisiblePassword"
            android:textSize="12sp" />

        <ImageButton
            android:id="@+id/bnSearch"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/inputSearch"
            android:layout_centerInParent="true"
            android:layout_toLeftOf="@+id/bnClear"
            android:background="@android:color/transparent"
            android:src="@drawable/search" />

        <ImageButton
            android:id="@+id/bnClear"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/inputSearch"
            android:layout_centerInParent="true"
            android:layout_marginRight="3dip"
            android:background="@android:color/transparent"
            android:onClick="ClearSearch"
            android:src="@drawable/cancel" />

        <Button
            android:id="@+id/imageButtonAddProject"
            android:layout_width="wrap_content"
            android:layout_height="35dp"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_centerInParent="true"
            android:drawableLeft="@drawable/add"
            android:onClick="addProject"
            android:text=" Add Project"
            android:textColor="@color/text_color"
            android:textSize="12sp" />
    </RelativeLayout>

    <!-- List View -->

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="337dp"
        android:layout_gravity="center"
        android:layout_weight="0.74" >

        <TextView
            android:id="@+id/emptyList"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="There is no item"
            android:textSize="25sp" />

        <ListView
            android:id="@+id/list" android:background="@android:color/white"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            >
        </ListView>

    </RelativeLayout>
   <!-- end of listview -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.04"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/bnPrevious"
            android:layout_width="80dp"
            android:layout_height="35dp"
            android:layout_marginLeft="30dp"
            android:background="@android:color/transparent"
            android:text="&lt;&lt; Prev"
            android:textSize="12sp" />

        <TextView
            android:id="@+id/page"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Page"
            android:textSize="12sp" />

        <EditText
            android:id="@+id/editpage"
            android:layout_width="wrap_content"
            android:layout_height="35dp"
            android:textSize="12sp" />

        <TextView
            android:id="@+id/of"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="of "
            android:textSize="12sp" />

        <TextView
            android:id="@+id/nbpage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="12sp" />

        <Button
            android:id="@+id/bnNext"
            android:layout_width="80dp"
            android:layout_height="35dp"
            android:layout_marginRight="30dp"
            android:background="@android:color/transparent"
            android:text="Next >>"
            android:textSize="12sp" />
    </LinearLayout>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

在代码中.

if (container == null) {
         lv.setVisibility(View.INVISIBLE);
}
Run Code Online (Sandbox Code Playgroud)

希望它会有用.


Die*_*mar 17

下面是一个带有示例的代码段

TextView emptyList = new TextView(getContext());
emptyList.setText("The list is empty");

ListView mListView = (ListView) findViewById(R.id.list_view);
((ViewGroup)mListView.getParent()).addView(emptyList);
mListView.setEmptyView(emptyList);
mListView.setAdapter(adapter);
Run Code Online (Sandbox Code Playgroud)