简体   繁体   中英

Showing progress dialog while getting data from firebase database

I am new to firebase and i want to show progress dialog while fetching data from firebase. I have achieved this but the problem is i cannot dismiss the dialog when some network error occurs while fetching. I know OnDataChange method is called when data is loaded without errors from firebase but what if some error occurs. Which method is called then?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM