Search code examples
androidandroid-listviewandroid-arrayadapterandroid-listfragmentroboguice

How to dismiss Android's progress bar when failed to load a ListView's contents?


I have a class extending RoboListFragment that uses a manager class to fetch some class' information (a Guest in my model). The problem I'm facing is when this manager fails to retrieve the data, I would like to dismiss the progress bar that is shown. Here are the most important parts of my Fragment:

public class GuestFragment extends RoboListFragment {

    // Lines omitted

    private void loadGuest() {
        Log.d(GuestFragment.class.getSimpleName(), "Loading guest...");
        this.guestManager.getGuest(new Callback<Guest>() {
            @Override
            public void onSuccess(Guest guest) {
                Log.d(GuestFragment.class.getSimpleName(), "Guest loaded OK");
                GuestFragment.this.createAdapter(guest);
            }

            @Override
            public void onFailure(UmError ex) {
                // Here I want to dismiss the ProgressBar
                Log.d(GuestFragment.class.getSimpleName(), "Guest failed");
                GuestFragment.this.getListView().setEmptyView(new View(getActivity()));     // This line wouldn't work
                Toast.makeText(GuestFragment.this.getActivity(), "Error here!", Toast.LENGTH_LONG);
            }
        });
    }

    private void createAdapter(Guest guest) {
        // Lines omitted
        if (guestExists && activityWasCreated) {
            ArrayAdapter adapter = new ProfilesAdapter(this.getActivity(), guest.getDeliveryProfiles());
            this.setListAdapter(adapter);
        }

    }
}

EDIT #1: I tried adding this line in my onFailure() method

GuestFragment.this.getListView().setAdapter(null)

but didn't work. EDIT #2: I also tried adding this in my onFailure() method

GuestFragment.this.getListView().setEmptyView(null);

didn't work either.

The XML is quite simple:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/pLarge"
android:orientation="vertical"
tools:context=".fragment.GuestFragment">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@id/android:list"
        android:divider="@android:color/transparent"
        android:dividerHeight="@dimen/fSmaller"/>
</LinearLayout>

As you can see, I'm not defining any ProgressBar at all. I've been told that this ProgressBar is shown by Android until the Adapter is set (I don't have further details), but I don't know how to set some empty content for the ListView

How could I achieve this, so that the ProgressBar hides when the Callback returns sucessfully? Best regards.


Solution

  • I was making a stupid mistake :(

    Instead of setting null to the List Adapter, like this (correct way):

    GuestFragment.this.setListAdapter(null)
    

    I was trying to get the ListView and set a null adapter, like this:

    GuestFragment.this.getListView().setAdapter(null)
    

    Thanks for your comments anyway!