首页 > 解决方案 > Android搜索过滤器在没有结果时显示文本视图

问题描述

我有一个列表视图和搜索视图。当搜索过滤器给出的结果与列表视图中的任何项目都不匹配时,我需要将 textview 显示为“Nothing present”。我怎样才能做到这一点?

这是我的 xml -

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:layout_width="match_parent"
             android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

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

            <SearchView
                android:id="@+id/search"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"/>
        </LinearLayout>

        <android.support.v4.widget.SwipeRefreshLayout
            android:id="@+id/swiperefresh"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">

            <ListView
                android:id="@android:id/list"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:clipToPadding="false"
                android:paddingBottom="60dp"/>

        </android.support.v4.widget.SwipeRefreshLayout>


    </LinearLayout>
</FrameLayout>

标签: javaandroid

解决方案


您可以做的是,在您的列表视图适配器上,当您使用的数组为空时,在您的 getItemCount 上,您返回 1,当您返回视图时,如果数组为空,您将返回一个带有“Nothing”的视图当下”。


推荐阅读