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

时间:2014-05-30 15:01:19

标签: java android listview android-annotations

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

我怎样才能做到这一点?

我尝试过使用两个ViewStubs(一个用于empty.xml,另一个用于error.xml),但是没有按预期工作,因为早期的ViewStub高于另一个ViewStub,我无法使用它。切换回来(也许我错误地使用了它们:/)

代码:

ProfileFragment.java

使用AndroidAnnotations进行注释。 showErrorView()是我正在寻找的功能,请记住,如果用户点击重试并且配置文件确实加载,则需要使用标准空视图(empty.xml)替换此错误视图(error.xml)< / p>

@EFragment(R.layout.fragment_profile)
public class ProfileFragment {

private final String TAG = this.getClass().getSimpleName();

@RestService
ProfileRest rest;

@Bean
ProfileAdapter adapter;

/**
 * The object this fragment will display
 */
private Profile profile;

@AfterInject
void afterInject() {

    // Add the API key to each request using the APIKeyInterceptor
    rest.setRestTemplate(Util.addAPIKey(keyInterceptor, rest.getRestTemplate()));

    // Retrieve and display the user's profile
    getProfile();
}

/**
 * - Bind an emptyView to this objects ListView
 * - Set the Adapter that handles the ListView
 * - Adds a click listener to handle clicks to each row in the list
 */
@AfterViews
void bindListView() {
    // here emptyView corresponds to empty.xml
    listView.setEmptyView(emptyView);   // Show the user the emptyView if the adapter is empty
    listView.setAdapter(adapter);       // bind adapter to listView to handle list items
    listView.setOnItemClickListener(clickListener); // Handle row clicks (show more info)
}

/**
 * Downloads the user's profile
 */
@Background
void getProfile() {

    showLoading();      // give the user a visual while we are reloading

    if ( !setProfileFromCache() && !setProfileFromNet() ) {
        // How do we show the error.xml in place of empty.xml?
        showErrorView();            
    }


    // Stop loading animation
    hideLoading();

}


/**
 * Attempts to set this Fragments profile via the cache
 * @return true if we are able to set the profile, false otherwise
 */
boolean setProfileFromCache()  {

    try {
        Medications cached = (Medications) cache.load(Medications.class);

        if (bandID.equals(cached.getUser())) {

            // Update UI with cached profile
            update(cached);

            Log.d(TAG, "profile retrieved from cache");

            return true;
        }
    } catch (Exception e) {
        errorHandler.handle(e);
    }

    return false;
}

/**
 * Attempts to set this Fragments profile via the REST API
 * @return true if we are able to set the profile, false otherwise
 */
boolean setProfileFromNet()  {

    try {

        // Hit the web and download the profile
        Medications response = rest.get(bandID);
        Log.d(TAG, "profile downloaded from the net");

        // Cache response
        cache(response);

        // Update UI with response
        update(response);

        return true;

    } catch (HttpStatusCodeException e) {

        errorHandler.handle(e);     // Handle error

    } catch (Exception e) {

        errorHandler.handle(e);     // Handle error

    }

    return false;
}

@UiThread
void update(Profile profile) {

    this.profile = profile;

    // Update adapter to deal with new data
    adapter.update(profile);
}
}

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()

<?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>

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>

0 个答案:

没有答案