【问题标题】:Async task progress dialog take long time for simple task in android异步任务进度对话框需要很长时间才能完成 android 中的简单任务
【发布时间】:2019-03-13 12:41:34
【问题描述】:

当我调用这个异步任务时,它需要很长时间,但是任务很短,当我直接输入代码时它工作正常,但是当我使用异步任务时它需要时间,我已经从这个站点找到了解决方案,
Progress dialog async task taking longer time than expected 但它不能正常工作,

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

请帮助我,在此先感谢

【问题讨论】:

  • 你把重要的部分删掉了……
  • @TheWanderer 我更新了代码,知道的请检查..
  • 没有必要把它打包到AsyncTask 中。 Firebase 代码已经是异步的。数据将呈现在onDataChange()
  • @Barns 那么我如何显示进度对话框?

标签: android android-asynctask progressdialog


【解决方案1】:

试试这样的:

ProgressDialog创建一个类实例

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 的代码。我只是假设它已经起作用了。

【讨论】:

  • 非常感谢,现在比以前更好了,:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-10
  • 2012-10-09
相关资源
最近更新 更多