如何在FirebaseListAdapter完成时收到通知?

时间:2016-01-24 22:32:30

标签: android listview firebase progressdialog firebaseui

我使用FirebaseListAdapter并且我需要显示ProgressDialog,但我不知道如何知道它何时完成加载。

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


}

我什么时候可以放置ProgressDialog.dismiss()?

2 个答案:

答案 0 :(得分:12)

Firebase建立在同步数据的前提下。出于这个原因,完成同步从来没有真正的时刻。

一次性下载某个位置的初始数据集,并为每个孩子调用D9iJyWEHuEmuEmsRm9hTkapTCrk,然后调出一个onChildAdded()。如果要在加载此初始数据时隐藏进度条,可以为单个事件注册onDataChanged()

ValueEventListener

在上面的代码段中,所有孩子都已经添加到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) { } }); 。根据您的使用情况,这可能是隐藏进度条的正确时间,或者可能是您想要的时间。

如果您想知道第一个数据何时被渲染,您可以使用适配器注册一个观察者:

ListView

一旦第一个数据从Firebase到达,您就可以使用此方法隐藏进度条。

答案 1 :(得分:1)

自2017年1月起FirebaseListAdapter有以下方法可以覆盖(由github上的Frank提交):

  /**
     * 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.
     * <p>
     * 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.
     */
    protected void onDataChanged() {
    }