Calling methods from another class

Asked

Viewed 300 times

0

In case I need to call a method of another class to perform an insertion in the database and I have to pass an object of type Usuario as a parameter

Code:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.usuario);

    Button btn_gravar = (Button)findViewById(R.id.btn_gravar);

    EditText nome_usuario = (EditText)findViewById(R.id.edt_nome);
    EditText sexo_usuario = (EditText)findViewById(R.id.edt_sexo);
    EditText telefone_usuario = (EditText)findViewById(R.id.edt_telefone);
    EditText datanascimento_usuario = (EditText)findViewById(R.id.edt_datanasc);
    EditText datacadastro_usuario = (EditText)findViewById(R.id.edt_datacad);
    EditText cidade_usuario = (EditText)findViewById(R.id.edt_cidade);
    EditText endereco_usuario = (EditText)findViewById(R.id.edt_endereco);
    EditText cpf_usuario = (EditText)findViewById(R.id.edt_cpf);
    EditText rg_usuario = (EditText)findViewById(R.id.edt_rg);
    EditText email_usuario = (EditText)findViewById(R.id.edt_email);

    final Usuario usuario = new Usuario();
            usuario.nome = nome_usuario;
            usuario.sexo = sexo_usuario;
            usuario.telefone = telefone_usuario;
            usuario.datanascimento = datanascimento_usuario;
            usuario.datacadastro = datacadastro_usuario;
            usuario.cidade = cidade_usuario;
            usuario.endereco = endereco_usuario;
            usuario.cpf = cpf_usuario;
            usuario.rg = rg_usuario;
            usuario.email = email_usuario;

    btn_gravar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Passando parâmetros da intent para o obj usuário
            Toast.makeText(getApplicationContext(), "Você foi cadastrado com sucesso!!!", Toast.LENGTH_LONG).show();

                CrudUsuario crudusuario;
                crudusuario = new CrudUsuario(usuario);
                crudusuario.insert(usuario);



        }
    });

} 

1 answer

1


How is this method stated in the other class? Don’t you want to instantiate it in your code? Why the most "correct" way is for you to declare a class instance inside.

If you don’t want to instantiate a way is to declare the method as static there in the class where the method is written, then you don’t need to declare the class, you just need to really import. Of course, on the other side he has to be ready to receive an object as a parameter.

  • The object must feed the method of the Crusading Class public void Insert(User user) { //My logic }

  • What mistake is there? Remembering that if you pass a user object as parameter there on the other side the User class has to be prepared to instantiate the User object.

Browser other questions tagged

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