dehq dehq - 5 months ago 50
Android Question

How to be notified when FirebaseListAdapter finishes?

I'm using the FirebaseListAdapter and I need to display a ProgressDialog, but I don't know how to know when it finishes loading.

public class TabFragment1 extends ListFragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tab_fragment_1, container, false);
ListView eventosView = (ListView) rootView.findViewById(android.R.id.list);
Firebase ref = new Firebase(//myappurl);
Firebase usersRef = new Firebase(//myappurl/eventos/users);
Singleton singletonUser = Singleton.getInstance();
setUser(singletonUser);

Firebase ref = new Firebase(//myappurl/eventos);
FirebaseListAdapter<Evento> mAdapter = new FirebaseListAdapter<Evento>(getActivity(), Evento.class, R.layout.event_row, ref) {
@Override
protected void populateView(View view, Evento evento) {
((TextView)view.findViewById(R.id.eventTitle)).setText(evento.getNome());
((TextView)view.findViewById(R.id.eventPlace)).setText(evento.getLocal());
}
};
setListAdapter(mAdapter);
return mAdapter;


return rootView;
}

private void setUser(Singleton singletonUser){
singletonUser.getToken();
singletonUser.setUuid();
singletonUser.getUuid();
}


}


When can I place the ProgressDialog.dismiss()?

Answer

Firebase is built on the premise of synchronizing data. For this reason there is never really a moment when it is done synchronizing.

The initial set of data for a location is downloaded in one go and results in a call to onChildAdded() for each child, followed by a single onDataChanged(). If you want to hide the progress bar when this initial data has loaded, you can register a ValueEventListener for a single event:

ref.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        // the initial data has been loaded, hide the progress bar
    }

    @Override
    public void onCancelled(DatabaseError firebaseError) {

    }
});

In the above snippet, all the children will have been added to your ListView already. Depending on your use-case, this may be the right time to hide the progress bar or it may be later than wanted.

If you want to know when the first data has been rendered, you can register an observer with your adapter:

adapter.registerDataSetObserver(new DataSetObserver() {
    @Override
    public void onChanged() {
        super.onChanged();
        // the first time you get here, hide the progress bar
    }

    @Override
    public void onInvalidated() {
        super.onInvalidated();
    }
});

You could use this approach to hide the progress bar as soon as the first data has arrived from Firebase.