Problem with ToString in Android Studio

0

I have variables of type String and variables of type Int. When I do getText.ToString() in a variable of type int Android Studio presents error because the type of variable is not a String.

Would anyone know which option I can use for Int type variables?

packagestudio.com.projetofinal;importandroid.content.DialogInterface;importandroid.content.Intent;importandroid.support.v7.app.AlertDialog;importandroid.support.v7.app.AppCompatActivity;importandroid.os.Bundle;importandroid.view.View;importandroid.widget.Button;importandroid.widget.EditText;importjava.util.List;publicclassCadastroActivityextendsAppCompatActivity{privateDBcadastrodh;EditTextetnome,etcpf,etidade,ettelefone,etemail;ButtonInserirDB;ButtonListarCadastro;Buttonbtvoltar;@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_cadastro);btvoltar=(Button)findViewById(R.id.voltar);btvoltar.setOnClickListener(newView.OnClickListener(){@OverridepublicvoidonClick(Viewv){voltarHome();}});this.dh=newDBcadastro(this);etnome=(EditText)findViewById(R.id.etnome);etcpf=(EditText)findViewById(R.id.etcpf);etidade=(EditText)findViewById(R.id.etidade);ettelefone=(EditText)findViewById(R.id.ettelefone);etemail=(EditText)findViewById(R.id.etemail);InserirDB=(Button)findViewById(R.id.InserirDB);ListarCadastro=(Button)findViewById(R.id.ListarCadastro);InserirDB.setOnClickListener(newView.OnClickListener(){@OverridepublicvoidonClick(Viewv){if(etnome.getText().length()>0&&etcpf.getText().length()>0&&etidade.getText().length()>0&&ettelefone.getText().length()>0&&etemail.getText().length()>0){dh.insert(etnome.getText().toString(),etcpf.getText().toString(),etidade.getText().toString(),ettelefone.getText().toString(),etemail.getText().toString());AlertDialog.Builderadb=newAlertDialog.Builder(CadastroActivity.this);adb.setTitle("Sucesso");
                    adb.setMessage("Cadastro Realizado!");
                    adb.show();

                    etnome.setText("");
                    etcpf.setText("");
                    etidade.setText("");
                    ettelefone.setText("");
                    etemail.setText("");

            }else {
                    AlertDialog.Builder adb = new AlertDialog.Builder(CadastroActivity.this);
                    adb.setTitle("Erro");
                    adb.setMessage("Todos os campos devem ser preenchidos!");
                    adb.show();

                    etnome.setText("");
                    etcpf.setText("");
                    etidade.setText("");
                    ettelefone.setText("");
                    etemail.setText("");
                }
        }

    });

     ListarCadastro.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {

             List<cadastro> cadastro = dh.queryGetALL();
             if(cadastro == null){
                 AlertDialog.Builder adb = new AlertDialog.Builder(CadastroActivity.this);
                 adb.setTitle("Mensagem");
                 adb.setMessage("Não há registros cadastrados!");
                 adb.show();

                 etnome.setText("");
                 etcpf.setText("");
                 etidade.setText("");
                 ettelefone.setText("");
                 etemail.setText("");
                 return;
             }

             for (int i=0;i<cadastro.size();i++){
                 cadastro cadsatro = (cadastro) cadastro.get(i);
                 AlertDialog.Builder adb = new AlertDialog.Builder(CadastroActivity.this);
                 adb.setTitle("Registro "+ i);
                 adb.setMessage("Nome: "+ cadsatro.getNome()+ "\nCPF: "+ cadsatro.getCpf() + "\nIdade: "+ cadsatro.getIdade() +
                         "\nTelefone: " + cadsatro.getTelefone() + "\n Email: " + cadsatro.getTelefone());
                 adb.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                     @Override
                     public void onClick(DialogInterface dialog, int which) {
                         dialog.dismiss();
                     }
                 });

                 adb.show();
             }


         }


     });


    void voltarHome(){
        Intent intent = new Intent();
        intent.setClass(CadastroActivity.this, MainActivity.class);
        startActivity(intent);
        finish();
    }
}
    
asked by anonymous 24.06.2018 / 17:01

1 answer

1

Android Studio is complaining that the insert() method expects 3 parameters of type int , but you are passing them as String , precisely because you use the toString() method. It does not make much sense.

In this case, you have to use the wrapper wrapper class, more specifically the% wrapper method, which will grab the% wrapper content and convert it to an integer value, returning Integer :

Integer.parseInt(campo.getText().toString())
    
24.06.2018 / 18:45