dimanche 19 avril 2015

Progress bar inside custom dialog fragment - How to pass progress from an AsyncTask?

I have created a custom dialog fragment, inside it I have placed a progress bar. The host activity has an AsyncTask which uploads some images to a server. What I need is a way to pass the progress to the progress dialog fragment, I have tried via variables but it launches the fragmet again and again as the progress changes.


How could I pass the progress values without re launching the dialog fragment while values are changing?


I have tried with an interface as well, but it won't initialise my listener (the listener is always null).


my asyncTask (loopj):


private UploadProgressListener progressListener;



public interface UploadProgressListener {
public void onProgress(int position, int length);
}

//Interface to send selected image's position for deletion
public void setUploadProgressListener(UploadProgressListener listener) {
this.progressListener = listener;
}


....



client.post("http://ift.tt/1G7kFqj", params,
new AsyncHttpResponseHandler() {

@Override
public void onFailure(int arg0, Header[] arg1, byte[] arg2,
Throwable arg3) {
System.out.println("Upload failed!" + arg3
+ " statys code: " + arg0);
if (arg0 == 0) {
System.out.println("Retrying Upload!");
sendZip();
}
}

@Override
public void onProgress(int position, int length) {

if(progressListener != null){
progressListener.onProgress(position, length);
}

}

@Override
public void onStart() {

// Show preloader dialog
DialogFragment newFragment = new PreloaderDialog();
newFragment.show(getSupportFragmentManager(),
"preloader");
}

@Override
public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
System.out.println("Success!");
Toast.makeText(context, "Multumim pentru Comanda!",
Toast.LENGTH_LONG).show();
finish();
datasource.deleteAllRows();
}
});


and the progress dialog fragment:



public class PreloaderDialog extends DialogFragment implements UploadProgressListener {

private Builder v;
private ProgressBar uploadProgress;
int position;
int length;

/*
public PreloaderDialog() {
this.position = position;
this.length = length;
}
*/

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.preloader_dialog, null);

builder.setView(view);

OrderActivity progressChanged = new OrderActivity();
progressChanged.setUploadProgressListener(new UploadProgressListener() {

@Override
public void onProgress(int position, int length) {
System.out.println("changed");

}
});

uploadProgress = (ProgressBar) view.findViewById(R.id.uploadProressbar);

// Create the AlertDialog object and return it
return builder.create();
}


@Override
public void onProgress(int position, int length) {
//uploadProgress.setMax(length);
// uploadProgress.setProgress(position);
//System.out.println("progresspos: " + position);

}

Aucun commentaire:

Enregistrer un commentaire