Question

My problem is this;

I have a AsyncTask that works fine, and on doInBackground() it calls a new class that sync my data to a web service using REST service, i don't have everything on a unique class because i need the same content sync for different activitys and it's easier this way.

What i need is, on the sync procedure, i can get the number of "contacts" and everytime it downloads a contact, removes 1 from the "contacts" lenght, so, i nedd to show on the progress dialog the length of contact and refresh everytime it downloads a new "contact"

hre's my code for the AsyncTask:

public class syncContentTask extends AsyncTask<String, String, Boolean> {

private ProgressDialog mprogress;
private Context context;

//token for JSON header to authenticate
String authToken;

public syncContentTask(Context cxt, String token) {
    this.context = cxt;
    mprogress = new ProgressDialog(context);
    authToken = token;
}

protected void onPreExecute() {
    mprogress = ProgressDialog.show(context, "Sync", "Sync in progress...");
}

@Override
protected Boolean doInBackground(String... params) {
    syncData syncData = new syncData();
    syncData.syncData(context, authToken);
    publishProgress(progress);
    return true;
}

protected void onProgressUpdate(String... progress) {
    //mprogress.setProgress(Integer.parseInt(progress[0]));
}

protected void onPostExecute(Boolean result) {
    if (result) {
        mprogress.dismiss();
    }
}

}

In the Sync Data class i have functions that handles the HttpRequest and database stuff...

can anyone help??

Was it helpful?

Solution

You need to create a listener for your data progress and have it update the progress bar. Right now it looks like this line:

syncData.syncData(context, authToken);

blocks and no updates are provided to your progress indicator until it is done. So, you need something like:

MyListener listener = new MyListener(context);
SyncData syncData = new syncData(listener);

And in your listener have callback methods like myListener.downloadStarted() , myListener.updateProgressBar(int progress) and myListener.downloadCompleted() that your syncData class calls as the download progresses.

For example:

public abstract class SDScanAdapter implements SDScanListener {
    public void startScan() {
    }
    public void updateScanProgress(int scanItemsTotal, int scanItemsCompleted) {
    }
    public void scanComplete() {
    }
}

Then create a listener class:

public class ScanListener extends SDScanAdapter {
    @Override
    public void scanComplete(String contactName, String action) {
        runOnUiThread(scanComplete);
    }

    @Override
    public void startScan() {
        runOnUiThread(startScan);
    }

    @Override
    public void updateScanProgress(int scanItemsTotal,
            int scanItemsCompleted) {
        if (scanCountTotal != scanItemsTotal) {
            scanCountTotal = scanItemsTotal;
            progressBar.setMax(scanCountTotal);
        }
        if (scanCountUpdate != scanItemsCompleted) {
            scanCountUpdate = scanItemsCompleted;
            runOnUiThread(updateScanProgress);
        }
    }
}

And then for this example you need Runnables (startScan, scanComplete and updateScanProgress) that perform UI tasks, like updating the progress bar. In your case, you may also want to load some of the results, etc.

Then in your AsyncTask you do:

ScanListener listener = new ScanListener();
SyncData syncData = new syncData(listener);

Assuming the SDScanListener class and AsyncTask are all in your Activity. Also, your SyncData calss will need to have a SDScanListener variable that is set when it instantiates. Then, while it does its job, calls are made to the listener methods like:

scanListener.startScan();

And while it progresses, it calls the other methods (and corresponding parameters are passed in).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top