从Firebase检索照片时未显示进度对话框

时间:2017-03-31 01:10:58

标签: android firebase progressdialog firebase-storage

正如标题所说,当我尝试从Firebase存储中检索一些照片时,我的进度对话框没有显示。 这是我的代码:

progress = ProgressDialog.show(getActivity(), "Loading...", "Please wait...", true);
//Retrieve photos from storage
mDatabase.child("TopCommentPhotos").addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {

        Iterable<DataSnapshot> children = dataSnapshot.getChildren();
        Random r = new Random();
        int random = r.nextInt((size+1) - 1) + 1;

        int count=0;
        for(DataSnapshot child: children) {
            count = count+1;
            if (count == random){
                progress.dismiss();
                ImageModel imageModel = child.getValue(ImageModel.class);
                String downloadUrl = imageModel.getUrl();
               // Display photo
                Glide.with(getActivity())
                        .load(downloadUrl)
                        .into(mImageView);

            }
        }

    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        Toast.makeText(getActivity(),"Error",Toast.LENGTH_LONG).show();

    }
});

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

public void onDataChange(DataSnapshot dataSnapshot) {

                                Iterable<DataSnapshot> children = dataSnapshot.getChildren();
                                Random r = new Random();
                                int random = r.nextInt((size+1) - 1) + 1;

                                int count=0;
                                for(DataSnapshot child: children) {
                                    count = count+1;
                                    if (count == random){

                                        ImageModel imageModel = child.getValue(ImageModel.class);
                                        String downloadUrl = imageModel.getUrl();
                                       // Display photo 
                                        Glide.with(getActivity()) 
                                                .load(downloadUrl)
                                                .into(mImageView); 

                                    } 
                                } 
progress.dismiss(); 


                            }

@Override 
                            public void onCancelled(DatabaseError databaseError) {
                                Toast.makeText(getActivity(),"Error",Toast.LENGTH_LONG).show();
progress.dismiss();
                      }