0

I have same stock item , I want to send local database to ApiService, But when I send also I want to update ProgressBar message. I tried the code below but it just shows when all proccessing is finishing.

  ProgressDialog progress= new ProgressDialog(this);        
  progress.setTitle(getResources().getString(R.string.progress_exporting));
  progress.setMessage("0/0");

when click button I call below method

  public void Export() {

    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            findViewById(R.id.btnExportOnlineWithStocktaking).setEnabled(false);
            progress.show();
        }
    });

    UpdateUI(send, total);

    try {

        switch (_stocktakingType) {
         case Division: {
                switch (_onlineExportType) {
                    case Item: {
                        isExport = ExportDivisionStocktakingItems(stocktakingId);
                    }
                    break;
                }
        } catch (Exception ex) {
    }

  }

// ExportDivisionStocktaking method

 public boolean ExportCustomStocktakingItems(int stocktakingId) {
     result = Boolean.parseBoolean(SendCustomStocktakingItems(stocktakingId,countResults).responseString);
}

My call back method

public ResponseModel SendCustomStocktakingItems(int selectedDivision, List<ExtensionServiceStocktakingItem> countResults) throws ExecutionException, InterruptedException {

    return new SendCustomStocktakingItemsService(flag -> true).execute(String.valueOf(selectedDivision), countResults.toString()).get();

}

//AsyncTask method

 public class SendDivisionStocktakingItemsService extends AsyncTask<String, Void, ResponseModel> {

    public AsyncResponseSendDivisionStocktakingItems delegate = null;

    public SendDivisionStocktakingItemsService(AsyncResponseSendDivisionStocktakingItems delegate) {
        this.delegate = delegate;
    }

    @Override
    protected ResponseModel doInBackground(String... parameters) {


        RequestHandler requestHandler = new RequestHandler();

        JSONObject params = new JSONObject();
        try {
            params.put("stocktakingItems", parameters[1]);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        ResponseModel responseModel = requestHandler.getRequestPostString(UHFApplication.getInstance().apiUrl
                        + "/api/MobileService/SendDivisionStocktakingItemsPost?stocktakingID="
                        + parameters[0],
                parameters[1]);
        return responseModel;

    }

    @Override
    protected void onPreExecute() {
        UpdateUI(send,total);
        super.onPreExecute();
    }

    @Override
    protected void onPostExecute(ResponseModel responseModel) {

        super.onPostExecute(responseModel);

        if (HttpURLConnection.HTTP_OK == responseModel.httpStatus) {

            delegate.processFinish(true);


        } else {
            delegate.processFinish(false);
        }
    }

}

//UICalled method

 public void UpdateUI(int send, int total) {
    runOnUiThread(() -> {
        progress.setMessage(send + "/" + total);
        Log.d("Send Data : ", send + "/" + total);

        if (send == total) {
            progress.dismiss();
            Toast.makeText(getApplicationContext(), "Succsess", Toast.LENGTH_SHORT).show();
        }
    });
}

//Update

//Ok I have a simle example how can I use. Below code when I click button I wan to open progress firstly and after that for loop is working and update progres message. I try it but not working.

Firstly For loop is working and after that progres opened.

 public void ExportTry(){
    UpdateUI(send,total);
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            btnExport.setEnabled(false);
            progress.show();
        }
    });


    for(int i=0;i<1000000;i++){
        UpdateUI(i,1000000);
    }
}
artemitSoft
  • 286
  • 5
  • 17

1 Answers1

0

You are missing the part of AsyncTask that will allow you to show progress messages while doInBackground is running. Take a look at onProgressUpdate and publishProgress on the same page.

publishProgress

void publishProgress (Progress... values)

This method can be invoked from doInBackground(Params...) to publish updates on the UI thread while the background computation is still running. Each call to this method will trigger the execution of onProgressUpdate(Progress...) on the UI thread. onProgressUpdate(Progress...) will not be called if the task has been canceled.

Community
  • 1
  • 1
Cheticamp
  • 61,413
  • 10
  • 78
  • 131
  • I use it but not working. Because I call a lot of times this asyncTask method. – artemitSoft Mar 31 '17 at 12:38
  • @artemitSoft Where do you use `publishProgress()`? I don't see it in the code you have posted. It has to be a method of `AsyncTask` and invoked from `doInBackground()`. – Cheticamp Mar 31 '17 at 12:48
  • Yes, I delete it because I call AsyncTask more than one in for() iteration than when I use in doInBackground() method Open a lot of time dialog. – artemitSoft Mar 31 '17 at 13:08
  • @artemitSoft Are you saying that you don't want to use `publishProgress` because it restarts the dialog a lot of times because you invoke the `AsyncTask` a lot of times? So, you want to accomplish showing progress without resorting to `publishProgress`? – Cheticamp Mar 31 '17 at 13:14
  • Yes, I have a for loop for send all stock send from local database to serviceApi via AsyncTask class. So when I click export button firstly I wan to show progress.message(0/150) and show. when I call AsyncTask class is return bool value and after that my progress.message(1/150) show – artemitSoft Mar 31 '17 at 13:20
  • @artemitSoft No difference, though. Don't set up the progress dialog within `AsyncTask` - set up the progress dialog before calling `AsyncTask` for the first time. Use `onPostExecute()` to update the progress dialog - 1, 2,3, etc. Dismiss the dialog after the last invocation of `AsyncTask` completes. This will be easiest if `AsyncTask` is an inner class, but you can also do it as a separate class with some callbacks. I hope that helps. – Cheticamp Mar 31 '17 at 13:39
  • I use your method but after the all operations is finished opened progress dialog. :) – artemitSoft Mar 31 '17 at 14:42