从firebase数据库获取数据时显示进度对话框

时间:2017-05-01 02:36:06

标签: android firebase firebase-realtime-database

我是firebase的新手,我想在从firebase获取数据时显示进度对话框。我已经实现了这个,但问题是我在取出时发生一些网络错误时无法解除对话框。我知道在加载数据时没有来自firebase的错误就调用了OnDataChange方法,但是如果发生了一些错误会怎么样。那么叫哪种方法?

 View view = inflater.inflate(R.layout.fragment_stories, container, false);
    recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

    showProgressDialog("Please Wait...");
    firebaseDatabase = FirebaseDatabase.getInstance();
    databaseReference = firebaseDatabase.getReference("content");
    databaseReference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            Log.d(TAG,"On Data Change");
            if(mProgressDialog != null){
                mProgressDialog.dismissWithAnimation();
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Log.d(TAG,"On Cancelled");

            if(mProgressDialog != null){
                mProgressDialog.dismissWithAnimation();
            }
        }
    });

1 个答案:

答案 0 :(得分:3)

private ProgressDialog mProgressDialog = new ProgressDialog(this);

mProgressDialog.setMessage("Work ...");
mProgressDialog.show();

FirebaseDatabase.getInstance().getReference()
                                    .child("child node")
                                    .addListenerForSingleValueEvent(new ValueEventListener() {
                                        @Override
                                        public void onDataChange(DataSnapshot dataSnapshot) {

                                            if (dataSnapshot.exists()){

                                               do something
                                               mProgressDialog.dismiss();
                                            } else {

                                               mProgressDialog.dismiss();
                                               Snackbar.make(v, "datasnapshot is null", Snackbar.LENGTH_LONG).setAction("Action", null).show();

                                            }
                                        }

                                        @Override
                                        public void onCancelled(DatabaseError databaseError) {

                                           mProgressDialog.dismiss();
                                        }
                                    });