Issue
This async task when i call , it take long time , but task is very short , when i directly put code it work ok , but when i use async task it take time , i already find solution from this site ,
Progress dialog async task taking longer time than expected
but it not work properly,
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();
}
}
this is main MainActivity calling code
Towing_TaskCollectAmountCash obj=new
Towing_TaskCollectAmountCash(CollectPayment.this,collectedAmount);
obj.execute();
Please help me , Thanks in advance
Solution
Try something like this:
Create a class instance for the ProgressDialog
private ProgressDialog progressDialog = null;
Now just a simple method for the Firebase
call:
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();
}
});
}
If you are calling this from inside an Activity
the context could be:
YourActivity.this
You don't seem to use the collectedAmount
parameter.
NOTE:
I have not check the code for querying Firebase. I will just assume it has worked is expected.
Answered By - Barns
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.