Android 当ListView为空且没有internet时显示自定义消息

Android 当ListView为空且没有internet时显示自定义消息,android,listview,Android,Listview,我知道有很多关于如何在ListView为空时显示消息的答案,但我想生成两条消息 没有数据时-标准空消息 当没有internet且我无法获取数据时-自定义无internet消息 以下是我的XML: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width=

我知道有很多关于如何在ListView为空时显示消息的答案,但我想生成两条消息

没有数据时-标准空消息 当没有internet且我无法获取数据时-自定义无internet消息 以下是我的XML:

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

    <ProgressBar
        android:id="@+id/main_progressbar"
        style="?android:attr/progressBarStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:visibility="gone" />

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="5dp"></ListView>

    <ViewStub
        android:id="@android:id/empty"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout="@layout/no_data_stub" />

</RelativeLayout>

正如您所看到的,这只是一个案例,它显示的是空消息,但我需要另一个。

以下是我如何做到的:

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

    <ProgressBar
        android:id="@+id/main_progressbar"
        style="?android:attr/progressBarStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:visibility="gone" />

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="5dp"></ListView>

    <TextView
        android:id="@+id/emptyTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_centerInParent="true"
        android:text="halo"/>

</RelativeLayout>

现在我可以设置我想要的任何文本,例如检查互联网连接。

好吧,我只为一条自定义消息设置了数据为空时,我不知道如何为两条自定义消息分隔它。无论如何,我已经编辑了这个问题,如果它有帮助的话。搜索如何检测互联网是否在Android上连接。已经回答了十几次了。然后根据其是否打开选择要使用的字符串。
 ListView listView = (ListView) rootView.findViewById(android.R.id.list);
    TextView emptyTextView = (TextView) rootView.findViewById(R.id.emptyTextView);
    emptyTextView.setText("test");
    listView.setEmptyView(emptyTextView);