Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dismiss a progress bar even if there is no view to populate in the FirebaseListAdapter?

I use the FirebaseUI FirebaseListAdapter. It takes some time to load the data and I want to show a spinning circle. I can dismiss the progress bar in by setting the view visibility to invisible in the populateView, but it doesn't work if there is no view to populate. How to handle this?

like image 775
Daniel Chan Avatar asked Jan 05 '23 04:01

Daniel Chan


1 Answers

Update: the FirebaseUI adapters nowadays have an onDataChanged() method that you can override to detect when they're done loading a set of data.

See the source code on github. From there:

This method will be triggered each time updates from the database have been completely processed. So the first time this method is called, the initial data has been loaded - including the case when no data at all is available. Each next time the method is called, a complete update (potentially consisting of updates to multiple child items) has been completed.

You would typically override this method to hide a loading indicator (after the initial load) or to complete a batch update to a UI element.

The FirebaseUI sample app overrides onDataChanged() to hide its "loading" indicator:

public void onDataChanged() {
    // If there are no chat messages, show a view that invites the user to add a message.
    mEmptyListMessage.setVisibility(getItemCount() == 0 ? View.VISIBLE : View.GONE);
}

Do this ☝️

Don't do that 👇


Original answer

The FirebaseUI list adapters internally use a Firebase ChildEventListener. This listener only fires for relevant child events. If there are no children, no event will fire.

You can detect this situation by attaching an additional value listener to the reference/query that you pass into the adapter.

DatabaseReference list = mDatabase.child("messages");
showSpinner();
mAdapter = new FirebaseListAdapter<Message>(......, list) {
    void populateView(View view, Message message, int position) {
        // the first message was loaded
        hideSpinner();
    }
});
list.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        // the initial data is loaded (even if there was none)
        hideSpinner();
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        Log.w(TAG, "onCancelled", databaseError.toException());
    }
});
like image 111
Frank van Puffelen Avatar answered Jan 13 '23 15:01

Frank van Puffelen