此异步任务在我调用时需要花费很长时间,但是任务很短,当我直接放置代码时它可以正常工作,但是当我使用异步任务需要花费时间时,我已经从该站点找到了解决方案,
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();
请帮助我,在此先感谢
答案 0 :(得分:0)
尝试这样的事情:
为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的代码。我只是假设它已经奏效了。