android 中的简单任务异步任务进度对话框需要很长时间

Async task progress dialog take long time for simple task in android

当我调用这个异步任务时,它需要很长时间,但任务很短,当我直接输入代码时它工作正常,但是当我使用异步任务时它需要时间,我已经从这个网站找到解决方案,
但它不能正常工作,

public class Towing_TaskCollectAmountCash extends AsyncTask<Double,Void,Void>{

private ProgressDialog progressDialog;
Activity activity;


public Towing_TaskCollectAmountCash(Activity activity,Double collectedAmount) {
    this.activity = activity;
    this.collectedAmount=collectedAmount;
    progressDialog=new ProgressDialog(activity);
}

//progress Dialog Showing
@Override
protected void onPreExecute() {
    progressDialog.setTitle("Please Wait...");
    progressDialog.setCancelable(false);
    progressDialog.setIndeterminate ( true ) ;
    progressDialog.show();
}
@Override
protected Void doInBackground(Double... amount){
     referenceOfDriverWallets=FirebaseDatabase.getInstance().getReference().child("Wallet").child("Drivers").child(FirebaseAuth.getInstance().getCurrentUser().getUid());
     referenceOfDriverWallets.addListenerForSingleValueEvent(new ValueEventListener({
        @Override
        public void onDataChange(DataSnapshot dataSnapshot){
          if(dataSnapshot.child("TotalRideCollection").exists()){
              totalRideCollection= Double.valueOf(dataSnapshot.child("TotalRideCollection").getValue().toString());
            }
        }

         @Override
        public void onCancelled(DatabaseError databaseError) {
        }
});}

//Progress Dialog dismiss
@Override
protected void onPostExecute(Void aVoid) {
    progressDialog.dismiss();
}

}

这是主要的 MainActivity 调用代码

Towing_TaskCollectAmountCash obj=new 
Towing_TaskCollectAmountCash(CollectPayment.this,collectedAmount);
obj.execute();

请帮助我,在此先感谢

尝试这样的事情:

ProgressDialog

创建一个 class 实例
private ProgressDialog progressDialog = null;

现在只是 Firebase 调用的简单方法:

private Void getData(){
    if(progressDialog == null){
        progressDialog = new ProgressDialog(context);
        progressDialog.setTitle("Please Wait...");
        progressDialog.setCancelable(false);
        progressDialog.setIndeterminate ( true ) ;
    }
    progressDialog.show();

     referenceOfDriverWallets=FirebaseDatabase.getInstance().getReference().child("Wallet").child("Drivers").child(FirebaseAuth.getInstance().getCurrentUser().getUid());
     referenceOfDriverWallets.addListenerForSingleValueEvent(new ValueEventListener({
        @Override
        public void onDataChange(DataSnapshot dataSnapshot){
          if(dataSnapshot.child("TotalRideCollection").exists()){
              totalRideCollection= Double.valueOf(dataSnapshot.child("TotalRideCollection").getValue().toString());

              progressDialog.dismiss();
            }
        }

         @Override
        public void onCancelled(DatabaseError databaseError) {
            progressDialog.dismiss();
        }
    });
}

如果您从 Activity 内部调用它,上下文可能是:

YourActivity.this

您似乎没有使用 collectedAmount 参数。



注意:

我没有查看查询Firebase的代码。我只是假设它已经正常工作了。