Tostring problem in Android Studio

Asked

Viewed 490 times

0

I have String type variables and Int type variables. When I do getText.ToString() in a variable of type int Android Studio shows error due to variable type not being a String.

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

inserir a descrição da imagem aqui

package studio.com.projetofinal;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import java.util.List;

public class CadastroActivity extends AppCompatActivity {

   private DBcadastro dh;
    EditText etnome, etcpf, etidade, ettelefone, etemail;
    Button InserirDB;
    Button ListarCadastro;


    Button btvoltar;

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

        btvoltar = (Button) findViewById(R.id.voltar);
        btvoltar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                voltarHome();
            }
        });

        this.dh = new DBcadastro(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(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                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.Builder adb = new AlertDialog.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();
    }
}
  • Probably in function dh.insert you should recer int, then do the parse: Integer.parseInt(campo.getText().toString()) or Integer.valueOf(campo.getText().toString()), I don’t remember

  • OK William Costamilam this solution you suggested worked.

1 answer

1


Android Studio is complaining that the method insert() wait 3 parameters of type int, but you’re passing them as String, precisely by using the method toString(). Doesn’t make much sense.

In that case, you have to use the class wrapper Integer, more specifically the method parseInt(), that will take the contents of EditText and convert it to an integer value, returning a int:

Integer.parseInt(campo.getText().toString())
  • Ok Statelessdev this solution you suggested worked.

  • If the answer helped you, mark it as correct to help future users who have a similar problem to yours.

Browser other questions tagged

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