Android Studio using AsyncTask with loading table

0

I'm with an app that I want to use AsyncTask so that when I do a task that takes some time, I'll load the message loading ... and once I finish this task disappear the message and continue to run the app normally. I have never used the Asynctask before, so I was experimenting but I have not been successful. In theory it seemed simple but it is more complex than I thought. I leave you the code that to see the structure, know that I am a novice in this.

Thank you very much.

public class RegistracionActivity extends AppCompatActivity {

private EditText inputFirstName, inputSurname, inputEmail, inputPass, 
inputMobile,
        inputPostCode, inputDateOfBirth;

class MyTask extends AsyncTask<Void, Void, String> {



    @Override
    protected void onPreExecute() {

        setupProgressDialog();
    }

    @Override
    protected String doInBackground(Void... voids) {

        registracion();  


        return "Terminado";
    }
}


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_registracion);

   // new MyTask().execute();

    setupActionBar();
    setupProgressDialog();
    setupInputs();


}

private void registracion(){

    UserApp usuario = new UserApp();
    dialog.show();

    String email = inputEmail.getText().toString();
    usuario.setEmail(email);
    String firstName = inputFirstName.getText().toString();
    usuario.setFirstName(firstName);
    String surName = inputSurname.getText().toString();
    usuario.setSurName(surName);
    String date = inputDateOfBirth.getText().toString();
    usuario.setDateOfBirth(date);
    String prefix = ccp.getSelectedCountryCodeWithPlus();
    String phone = inputMobile.getText().toString();
    usuario.setMobile(prefix+phone);
    String code = inputPostCode.getText().toString();
    usuario.setPostCode(code);
    usuario.setBalance(0.0);

    if(checkData(usuario)){

        registracionWithFirebase(user);
    }

}
Button bt1 = (Button) findViewById(R.id.register_button);
bt1.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {

            new MyTask().execute();
        }
    });

private void setupProgressDialog() {

    dialog = new ProgressDialog(this); // this = YourActivity
    dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    dialog.setMessage("Cargando. porfavor espere...");
    dialog.setIndeterminate(true);
    dialog.setCanceledOnTouchOutside(false);
}


}
    
asked by Chris 16.08.2018 в 16:09
source

1 answer

1

Very good,

Your code does not seem to be bad at all, but if something dirty and you lack something essential: In addition to creating the dialogue you must also show it at the moment you start to execute the AsyncTask and of course, you must also destroy it when the task you want to execute in another thread is completed ( AsyncTask ).

If you do not mind, I'm going to order your code a bit just to get a better idea of it and add the parts you need with comments so you know what it is.

public class RegistracionActivity extends AppCompatActivity {

    private EditText inputFirstName, inputSurname, inputEmail, inputPass, inputMobile, inputPostCode, inputDateOfBirth;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_registracion);

        setupActionBar();
        setupInputs();

        Button bt1 = (Button) findViewById(R.id.register_button);
        bt1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // Iniciamos la AsyncTask
                new MyTask().execute();
            }
        });
    }

    class MyTask extends AsyncTask<Void, Void, String> {

        ProgressDialog dialog;

        @Override
        protected void onPreExecute() {
            dialog = new ProgressDialog(RegistracionActivity.this);
            dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            dialog.setMessage("Cargando. porfavor espere...");
            dialog.setIndeterminate(true);
            dialog.setCanceledOnTouchOutside(false);
            dialog.show(); // Mostramos el di'alogo de cargando al crear la AsyncTask
        }

        @Override
        protected String doInBackground(Void... voids) {
            registracion();
            return "Terminado";
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            dialog.dismiss(); // Cerramos el di'alogo para continuar con la ejecuci'on normal de la app
            Log.d("RegistracionActivity", s); // Mostramos por consola el mensaje de Terminado que nos pasa la AsyncTask al finalizar
        }

        private void registracion(){

            UserApp usuario = new UserApp();
            dialog.show();

            String email = inputEmail.getText().toString();
            usuario.setEmail(email);
            String firstName = inputFirstName.getText().toString();
            usuario.setFirstName(firstName);
            String surName = inputSurname.getText().toString();
            usuario.setSurName(surName);
            String date = inputDateOfBirth.getText().toString();
            usuario.setDateOfBirth(date);
            String prefix = ccp.getSelectedCountryCodeWithPlus();
            String phone = inputMobile.getText().toString();
            usuario.setMobile(prefix+phone);
            String code = inputPostCode.getText().toString();
            usuario.setPostCode(code);
            usuario.setBalance(0.0);

            if(checkData(usuario)){
                registracionWithFirebase(user);
            }
        }
    }
}

I do not have the complete code so I have not been able to try it although I have used AsyncTask on numerous occasions and just as I have put it to you, it should work. Any questions you tell me:)

    
answered by 16.08.2018 в 19:02