Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/206.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用ListView处理失败的网络请求';空视图_Java_Android_Listview_Android Annotations - Fatal编程技术网

Java 使用ListView处理失败的网络请求';空视图

Java 使用ListView处理失败的网络请求';空视图,java,android,listview,android-annotations,Java,Android,Listview,Android Annotations,我有一个从网上检索数据的程序。我有一个空视图empty.xml(使用),用于ListView不包含任何项时。但是,我想在网络请求失败时设置另一个空视图(error.xml,我们称之为error视图)。如果网络请求失败并显示错误视图,我希望用户能够单击重试按钮,再次调用ProfileFragment.getProfile(),并将ListView空视图设置回默认空视图(empty.xml) 我怎样才能做到这一点 我尝试使用两个ViewStub(一个用于empty.xml,另一个用于error.xm

我有一个从网上检索数据的程序。我有一个空视图empty.xml(使用),用于ListView不包含任何项时。但是,我想在网络请求失败时设置另一个空视图(error.xml,我们称之为error视图)。如果网络请求失败并显示错误视图,我希望用户能够单击重试按钮,再次调用ProfileFragment.getProfile(),并将ListView空视图设置回默认空视图(empty.xml)

我怎样才能做到这一点

我尝试使用两个ViewStub(一个用于empty.xml,另一个用于error.xml),但这并没有按照预期工作,因为前面的ViewStub位于另一个ViewStub之上,我无法将它们切换回原来的位置(可能我使用错了:/)

代码: ProfileFragment.java 用AndroidAnnotations注释。淋浴RorView()是我正在寻找的功能,请记住,如果用户点击“重试”并加载配置文件,则需要将此错误视图(error.xml)替换为标准的空视图(empty.xml)

fragment_profile.xml ProfileFragment用于在我的应用程序中显示配置文件

<?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:orientation="vertical">

<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />

</LinearLayout>

error.xml 应在网络请求失败时显示。重试按钮应调用ProfileFragment.getProfile()


empty.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="#ffebebeb">

<ImageView
android:id="@+id/empty_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/shield_logo_empty"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:contentDescription="@string/empty_result" />

<TextView
android:id="@+id/error_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/empty_result"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/grey"
android:textStyle="bold"
android:layout_below="@+id/empty_logo"
android:layout_marginTop="30dp"
android:layout_centerHorizontal="true" />

</RelativeLayout>


我个人手动隐藏并显示适当的视图元素。我不知道任何嵌入方式。@Malachiasz当我试图将每个XML文件映射到某个屏幕时,我可能只需要将我的error.XML视图放在fragment_profile.XML中,并处理ListView和error视图的可见性
<?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="#ffebebeb">

<TextView
android:id="@+id/error_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:text="@string/error"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/grey"
android:textStyle="bold" />

<ImageView
android:id="@+id/frown"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/error_title"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:src="@drawable/frown"
android:contentDescription="@string/error_result" />

<TextView
android:id="@+id/error_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/frown"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="15dp"
android:text="@string/error_result"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/grey"
android:textStyle="bold" />

<Button
android:id="@+id/retry"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/error_text"
android:layout_centerHorizontal="true"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="30dp"
android:text="@string/retry" />
</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="#ffebebeb">

<ImageView
android:id="@+id/empty_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/shield_logo_empty"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:contentDescription="@string/empty_result" />

<TextView
android:id="@+id/error_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/empty_result"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/grey"
android:textStyle="bold"
android:layout_below="@+id/empty_logo"
android:layout_marginTop="30dp"
android:layout_centerHorizontal="true" />

</RelativeLayout>