Apply Asynctask to code that receives double vestments

Asked

Viewed 33 times

0

I have many doubts about how to apply the concept of AsyncTask. I’d like to know what that code would look like on AsyncTask. Thanks in advance

// Metodo da ação do botão Calcular 

public void Calculaar(View view) {

    new Thread() {
        public void run() {

            try {

                        String v1 = av1.getText().toString();
                        String v2 = av2.getText().toString();
                        String v3 = av3.getText().toString();
                        String v4 = av4.getText().toString();

                        if (v1.trim().isEmpty() || v2.trim().isEmpty() || v3.trim().isEmpty() || v4.trim().isEmpty()) {
                            AlertDialog.Builder dlg = new AlertDialog.Builder(Tela_Pres.this);
                            dlg.setMessage("Há Dados em Branco!");
                            dlg.setNeutralButton("Ok", null);
                            dlg.show();

                        } else {


                            AV1 = Double.parseDouble(av1.getText().toString());
                            AV2 = Double.parseDouble(av2.getText().toString());
                            AV3 = Double.parseDouble(av3.getText().toString());
                            AV4 = Double.parseDouble(av4.getText().toString());


                            resultado = ((AV1 * 2) + (AV2 * 2) + (AV3 * 1) + (AV4 * 1)) / 6;

                            DecimalFormat formato = new DecimalFormat("#.##");
                            resultado = Double.valueOf(formato.format(resultado));


                            if (resultado < 6) {


                                resultadoText.setText(String.valueOf("Sua Média: " + resultado + " , Aluno Reprovado"));
                                AlertDialog.Builder dlg = new AlertDialog.Builder(Tela_Pres.this);
                                dlg.setMessage("Resultado: " + resultado + " Aluno Reprovado");
                                dlg.setNeutralButton("Ok", null);
                                dlg.show();


                            } else {
                                resultadoText.setText(String.valueOf("Sua Média:  " + resultado + " , Aluno Aprovado"));

                                AlertDialog.Builder dlg = new AlertDialog.Builder(Tela_Pres.this);
                                dlg.setMessage("Resultado:   " + resultado + ", Parabéns Aluno Aprovado");
                                dlg.setNeutralButton("Ok", null);
                                dlg.show();
                            }
                        }


            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }.start();
}

1 answer

1

It is not recommended to change the interface from a thread, you should put everything that changes the user interface in the main thread. In your case you can validate the user input before calling Asynctask, and show the result in the method onPostExecute:

new AsyncTask<Double, Void, Double> () {
    public Double doInBackground(Double... params) {
        AV1 = params[0];
        AV2 = params[1];
        AV3 = params[2];
        AV4 = params[3];

        return ((AV1 * 2) + (AV2 * 2) + (AV3 * 1) + (AV4 * 1)) / 6;
    }

    public void onPostExecute(Double resultado) {
        if (resultado < 6) {
            resultadoText.setText("Sua Média: " + resultado + " , Aluno Reprovado");
            AlertDialog.Builder dlg = new AlertDialog.Builder(Tela_Pres.this);
            dlg.setMessage("Resultado: " + resultado + " Aluno Reprovado");
            dlg.setNeutralButton("Ok", null);
            dlg.show();

        } else {
            resultadoText.setText("Sua Média:  " + resultado + " , Aluno Aprovado");
            AlertDialog.Builder dlg = new AlertDialog.Builder(Tela_Pres.this);
            dlg.setMessage("Resultado:   " + resultado + ", Parabéns Aluno Aprovado");
            dlg.setNeutralButton("Ok", null);
            dlg.show();
        }
    }   

}.execute(Double.parseDouble(av1.getText().toString()), 
        Double.parseDouble(av2.getText().toString()),
        Double.parseDouble(av3.getText().toString()),
        Double.parseDouble(av4.getText().toString()));

But, unless this is just an example, it is not worth using a thread to do this calculation, because it is so simple that java will take longer to create the thread than to run it.

  • Your answer would look better if you explained why it is not recommended to change the graphical interface from another thread.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.