ProgressDialog does not work in Custom Listener

0

I have a custom Listener

public interface CustomListener {
    void processDownload(boolean isSuccess,int process,int total);
}

And the ControllerRequest class where I use the listener , and I have the onProcessDownload method that will be used to overwrite the listener methods used. . As in the active class, the listener customListener.processDownload (true, i, Total);

public class ControllerRequest implements Callback<ResponseClass> {

    ActivityMain activity;
    Databasedb db;
    Context context;
    CustomListener customListener;

    public void start(Context context, String ps) {

        activity = (ActivityMain) context;
        db = new Databasedb(context);

        APInterfaces interfaces = ApiClient.getClient("").create(APInterfaces.class);
        Call<ResponseClass> call = interfaces.doRequest(ps);
        call.enqueue(this);
    }

    @Override
    public void onResponse(Call<ResponseClass> call, Response<ResponseClass> response) {
        int statusCode = response.code();
        if (statusCode == 200) {
            if (response.body().getPermission().equals("danger")) {
                Toast.makeText(activity, "Error: " + response.body().getMessageWarning(), Toast.LENGTH_SHORT).show();
            } else if (response.body().getPermission().equals("isSuccesfull")) {
                int Total = response.body().getmyRequest().size();

                int i = 0;
                for (RequestClass myRequest : response.body().getmyRequest()) {
                    i++;
                    db.createTag(myRequest);
                    customListener.processDownload(true,i,Total);
                }
                db.close();
            }
        }
    }

    @Override
    public void onFailure(Call<ResponseClass> call, Throwable t) {
        t.printStackTrace();
    }
    public void onProcessDownload(CustomListener listener){
        customListener = listener;
    }
}

To finish and use the listener in another activity I use the onProcessDownload method as follows.

private ProgressDialog progress;
......
......
progress = new ProgressDialog(Activity.this);
...................
progress.setMessage("Sincronizando...");
            progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            progress.setCancelable(false);
            progress.setProgress(0)
 progress.show();
................
    myinstance.onProcessDownload(new CustomListener() {
                    @Override
                    public void processDownload(boolean isSuccess, int process, int total) {
                        //progress.setProgress(total);
                        Log.e("processDownload: ", "Descargando: "+process);
                        progress.setMax(total);
                        progress.setProgress(process);
                    }
                });

Up to here everything works correctly except:

progress.setProgress (process);

progress.setMax (total);

These two lines are the ones that do not work, they do not update the dialogue.

In the logcat the listener is what it shows me, indicating that the listener is listening to the changes that are generated from the other class, the problem would be only the dialogue.

*11-07 17:07:07.788 7348-7348/com.myapp.requestcomp E/processDownload:: Descargando: 556
11-07 17:07:07.797 7348-7348/com.myapp.requestcomp E/processDownload:: Descargando: 557
11-07 17:07:07.806 7348-7348/com.myapp.requestcomp E/processDownload:: Descargando: 558
11-07 17:07:07.815 7348-7348/com.myapp.requestcomp E/processDownload:: Descargando: 559
11-07 17:07:07.824 7348-7348/com.myapp.requestcomp E/processDownload:: Descargando: 560
11-07 17:07:07.833 7348-7348/com.myapp.requestcomp E/processDownload:: Descargando: 561
11-07 17:07:07.845 7348-7348/com.myapp.requestcomp E/processDownload:: Descargando: 562
11-07 17:07:07.862 7348-7348/com.myapp.requestcomp E/processDownload:: Descargando: 563
11-07 17:07:07.872 7348-7348/com.myapp.requestcomp E/processDownload:: Descargando: 564
11-07 17:07:07.884 7348-7348/com.myapp.requestcomp E/processDownload:: Descargando: 565
11-07 17:07:07.895 7348-7348/com.myapp.requestcomp E/processDownload:: Descargando: 566
11-07 17:07:07.906 7348-7348/com.myapp.requestcomp E/processDownload:: Descargando: 567
11-07 17:07:07.917 7348-7348/com.myapp.requestcomp E/processDownload:: Descargando: 568
11-07 17:07:07.935 7348-7348/com.myapp.requestcomp E/processDownload:: Descargando: 569
11-07 17:07:07.947 7348-7348/com.myapp.requestcomp E/processDownload:: Descargando: 570
11-07 17:07:07.957 7348-7348/com.myapp.requestcomp E/processDownload:: Descargando: 571
11-07 17:07:07.968 7348-7348/com.myapp.requestcomp E/processDownload:: Descargando: 572
11-07 17:07:07.979 7348-7348/com.myapp.requestcomp E/processDownload:: Descargando: 573
11-07 17:07:07.991 7348-7348/com.myapp.requestcomp E/processDownload:: Descargando: 574
11-07 17:07:08.000 7348-7348/com.myapp.requestcomp E/processDownload:: Descargando: 575
11-07 17:07:08.009 7348-7348/com.myapp.requestcomp E/processDownload:: Descargando: 576
11-07 17:07:08.017 7348-7348/com.myapp.requestcomp E/processDownload:: Descargando: 577
11-07 17:07:08.027 7348-7348/com.myapp.requestcomp E/processDownload:: Descargando: 578
11-07 17:07:08.038 7348-7348/com.myapp.requestcomp E/processDownload:: Descargando: 579
11-07 17:07:08.058 7348-7348/com.myapp.requestcomp E/processDownload:: Descargando: 580
11-07 17:07:08.068 7348-7348/com.myapp.requestcomp E/processDownload:: Descargando: 581
11-07 17:07:08.079 7348-7348/com.myapp.requestcomp E/processDownload:: Descargando: 582
11-07 17:07:08.091 7348-7348/com.myapp.requestcomp E/processDownload:: Descargando: 583
11-07 17:07:08.109 7348-7348/com.myapp.requestcomp E/processDownload:: Descargando: 584*

When closing the dialog and executing it again it shows this, but during the process it does not show anything, and of course when executing it again it does not change the information stays with the same data.

    
asked by Desarrollador Android Jr. 08.11.2017 в 00:37
source

1 answer

1

I'm not sure how to explain it, so I'll try to make it as clear as possible.

You are not synchronizing by percentage, but you are imitating a synchronization by percentage, which is obviously different. I say this for this part of the code:

if (statusCode == 200) {
{
 //...

Status 200 indicates that the server sent the response. A request can last an indefinite moment of time according to the speed of the user's Internet and your load dialog will be as if it was not synchronizing at zero, because a response from the server is expected. Once the server responds with the data it is when you try to emulate what it is loading when in fact it is in charge of the data with the response of the server.

That said, this is where the problem may be:

 int Total = response.body().getmyRequest().size();
    int Total = response.body().getmyRequest().size();

    int i = 0;
    for (RequestClass myRequest : response.body().getmyRequest()) {
        i++;
        db.createTag(myRequest);
        customListener.processDownload(true,i,Total);
    }
    db.close();

This is where you save the data and update the ProgressDialog according to the number of the iteration. The problem is that as the data is already in memory, this can happen fast, very fast and that may explain why you only see the end because it does not give the system the chance to update the percentage.

The best thing you can do is change a progress dialog of percentage for an indefinite one and that when you finish saving the data in the database, you close it and thus you avoid confusing us, the user and yourself .

    
answered by 08.11.2017 / 16:05
source