Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
173 views
in Technique[技术] by (71.8m points)

java - android AsyncTask in foreach

Have the following AsyncTask code:

private class checkChangesTask extends AsyncTask<String, Void, String> {
protected ProgressDialog mProgressDialog2; 
protected String _url = "", _idautor="", _idbook="";

@Override
protected void onPreExecute() {
    super.onPreExecute();
    this.mProgressDialog2 = new ProgressDialog(MainActivity.this);
    this.mProgressDialog2.setMessage("Check changes ...");
    this.mProgressDialog2.setIndeterminate(false);
    this.mProgressDialog2.setCanceledOnTouchOutside(false);
    this.mProgressDialog2.setCancelable(true);
    this.mProgressDialog2.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    this.mProgressDialog2.setMax(100);
    this.mProgressDialog2.setProgress(0);
    this.mProgressDialog2.show();
}       

@Override
protected String doInBackground(String... params) {
    Document doc = null;
    String _html = "";
    _idautor = params[0];
    _idbook = params[1];
    _url = params[2];
    try {
    doc = Jsoup.connect(_url).userAgent("Mozilla").get();
        Elements dd = doc.select("dd");

        int size = dd.size();
        int p = 1;
        for (Element src : dd) {
            this.mProgressDialog2.setProgress(p*100/size);
            if (p <= size-1){
                _html += src.outerHtml();
                ++p;
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    return Jsoup.clean(_html, Whitelist.basic());           
} 
@Override
protected void onPostExecute(String result) {   
    if(!result.equals("")){
        String lastfile = readPageFile(_idautor + "_" + _idbook);
        if(!lastfile.equals(result)){
            savePageToFile(_idautor + "_" + _idbook, result);
        }
    }else{
        Toast.makeText(MainActivity.this, "Error checkChangesTask", Toast.LENGTH_SHORT).show();
    }
    this.mProgressDialog2.dismiss();
}

the previous code I call in a loop:

public void checkChanges() {
    String[][] db_books = db.selectAllBOOKS();
    if (db_books.length>0){
        for (int j = 0; j < db_books.length; j++){  
            new checkChangesTask().executeOnExecutor(AsyncTask.SERIAL_EXECUTOR, db_books[j][1], db_books[j][0], db_books[j][2]);
        }
    }
}

Everything works fine, but the dialog does not display the correct value. First, it is worth it to 0% and then abruptly switches to 100%.

AsyncTask called in sequence (...executeOnExecutor(AsyncTask.SERIAL_EXECUTOR...).

If you run a AsyncTask not in the loop, all the displays are just perfect!

android: targetSdkVersion = "14"

I ask your help.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to use onProgressUpdate() inside the AsyncTask. Something like this (at a guess)

@Override
protected void onProgressUpdate(Integer... progress) {
    super.onProgressUpdate(progress);

    this.mProgressDialog2.setProgress(progress[0] * 100/progress[1]);
}

And replace this line:

this.mProgressDialog2.setProgress(p*100/size);

With this:

 publishProgress(new int[]{p,size})

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...