Calling a method in another Android class

Asked

Viewed 238 times

0

Good morning gentlemen, I’m with a doubt, I’m trying to call a method that is in another class but is not working, this same method already works if using another layout, just wanted to implement it in activity_main, could help me ?

Parameter Class code that is working:

public class Parametro extends AppCompatActivity implements View.OnClickListener{
    DadosOpenHelper db = new DadosOpenHelper(this);
    public EditText selecinfo;
    public EditText selecinfo2;
    public Button btncontador;
    public Button btncontador2;
    public Button btncontador3;
    public Button btnemail;
    public Button btnemitente;
    public Button btnemitente2;
    public Button btnemitente3;
    public Button btnimpressora;
    public Button btnimpressora2;
    public Button btnimpressora3;
    public Button btnrzaosocial;
    public Button btnnomefantasia;
    public Button btnie;
    public Button btncnpj;
    public Button gravar;
    public Button gravar2;

    public String tabela;
    public String descricao;
    public String retorno;


    public int valida = 1;

    @SuppressLint("WrongViewCast")
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main );
        DisplayMetrics dm = new DisplayMetrics();
        selecinfo = (EditText) findViewById(R.id.info);
        selecinfo2 = (EditText) findViewById(R.id.info2);
        btncontador = (Button) findViewById(R.id.buttoncontador);
        btncontador2 = (Button) findViewById(R.id.buttoncontador2);
        btncontador3 = (Button) findViewById(R.id.buttoncontador3);
        btnemail = (Button) findViewById(R.id.buttonemail);
        btnemitente = (Button) findViewById(R.id.buttonemitente);
        btnemitente2 = (Button) findViewById(R.id.buttonemitente2);
        btnemitente3 = (Button) findViewById(R.id.buttonemitente3);
        btnimpressora = (Button) findViewById(R.id.buttonimpressora);
        btnimpressora2 = (Button) findViewById(R.id.buttonimpressora2);
        btnimpressora3 = (Button) findViewById(R.id.buttonimpressora3);
        btnrzaosocial = (Button) findViewById(R.id.buttonrazaosocial);
        btnnomefantasia = (Button) findViewById(R.id.buttonnomefantasia);
        btnie = (Button) findViewById(R.id.buttonie);
        btncnpj = (Button) findViewById(R.id.buttoncnpj);
        gravar = (Button) findViewById(R.id.buttongravar1);
        gravar2 = (Button) findViewById(R.id.buttongravar2);

        btnnomefantasia.setOnClickListener(this);
        btnrzaosocial.setOnClickListener(this);
        btncnpj.setOnClickListener(this);
        btnie.setOnClickListener(this);
        btnemail.setOnClickListener(this);
        gravar2.setOnClickListener(this);
    }

    public void onClick(View v){
        switch (v.getId()){
            case R.id.btnnomefantasia:
                descricao = "nomefantasia" ;
                tabela = "emitente";
                retorno = "";
                String nomefantasia = "nomefantasia" ;
                SQLiteDatabase d1 = db.getReadableDatabase();
                Cursor cursor1 = d1.query(tabela , new String[]{descricao, nomefantasia},null, null, null   ,null,null,null);
                if (cursor1.moveToFirst()){

                    do{

                        retorno = cursor1.getString(0);
                        selecinfo2.setText(retorno);
                    }while (cursor1.moveToNext());
                }
                if(valida == 1){
                    gravar2.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            modelEmitente emit = new modelEmitente();
                            emit.setNomefantasia(selecinfo2.getText().toString());
                            db.addnomefantasia(emit);
                            Toast.makeText(getApplicationContext(), "Gravado Nome Fantasia", Toast.LENGTH_SHORT).show();
                        }
                    });
                }
                break;
            case R.id.btnrazaosocial:
                descricao = "razaosocial" ;
                tabela = "emitente";
                retorno = "";
                String razaosocial = "razaosocial" ;
                SQLiteDatabase d2 = db.getReadableDatabase();
                Cursor cursor2 = d2.query(tabela , new String[]{descricao, razaosocial},null, null, null   ,null,null,null);
                if (cursor2.moveToFirst()){

                    do{

                        retorno = cursor2.getString(0);
                        selecinfo2.setText(retorno);
                    }while (cursor2.moveToNext());
                }
                if(valida == 1){
                    gravar2.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            modelEmitente emit = new modelEmitente();
                            emit.setRazaosocial(selecinfo2.getText().toString());
                            db.addrazao(emit);
                            Toast.makeText(getApplicationContext(), "Gravado Razao Social", Toast.LENGTH_SHORT).show();
                        }
                    });
                }
                break;
            case R.id.btncnpj:
                descricao = "cnpj" ;
                tabela = "emitente";
                retorno = "";
                String cnpj = "cnpj" ;
                SQLiteDatabase d3 = db.getReadableDatabase();
                Cursor cursor3 = d3.query(tabela , new String[]{descricao, cnpj},null, null, null   ,null,null,null);
                if (cursor3.moveToFirst()){

                    do{

                        retorno = cursor3.getString(0);
                        selecinfo2.setText(retorno);
                    }while (cursor3.moveToNext());
                }
                if(valida == 1){
                    gravar2.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            modelEmitente emit = new modelEmitente();
                            emit.setCnpj(selecinfo2.getText().toString());
                            db.addcnpj(emit);
                            Toast.makeText(getApplicationContext(), "Gravado CNPJ", Toast.LENGTH_SHORT).show();
                        }
                    });
                }
                break;
            case R.id.btnie:
                descricao = "ie" ;
                tabela = "emitente";
                retorno = "";
                String ie = "ie" ;
                SQLiteDatabase d4 = db.getReadableDatabase();
                Cursor cursor4 = d4.query(tabela , new String[]{descricao, ie},null, null, null   ,null,null,null);
                if (cursor4.moveToFirst()){

                    do{

                        retorno = cursor4.getString(0);
                        selecinfo2.setText(retorno);
                    }while (cursor4.moveToNext());
                }
                if(valida == 1){
                    gravar2.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            modelEmitente emit = new modelEmitente();
                            emit.setIe(selecinfo2.getText().toString());
                            db.addie(emit);
                            Toast.makeText(getApplicationContext(), "Gravado IE", Toast.LENGTH_SHORT).show();
                        }
                    });
                }
                break;
            case R.id.btnemail:
                descricao = "emailcontador" ;
                tabela = "emitente";
                retorno = "";
                String emailcontador = "emailcontador" ;
                SQLiteDatabase d5 = db.getReadableDatabase();
                Cursor cursor5 = d5.query(tabela , new String[]{descricao, emailcontador},null, null, null   ,null,null,null);
                if (cursor5.moveToFirst()){

                    do{

                        retorno = cursor5.getString(0);
                        selecinfo2.setText(retorno);
                    }while (cursor5.moveToNext());
                }
                if(valida == 1){
                    gravar.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            modelEmitente emit = new modelEmitente();
                            emit.setEmailcontador(selecinfo.getText().toString());
                            db.addemail(emit);
                            Toast.makeText(getApplicationContext(), "Gravado Email", Toast.LENGTH_SHORT).show();
                        }
                    });
                }
        }
    }
}

Code where I’m trying to pull on mainactivity :

public void incluiparam(View view) {

       Parametro parameto = new Parametro();
       parameto.onClick(view);


    }

Remembering that I am trying to pull the "onClick", I do not know if it is possible, if someone can help me I am grateful.

  • and if you put the method as static? static

  • I tested with Static also but did not work

  • 1

    You’re trying to use a function of an Activity in another Activity is this ? you can talk more of the problem maybe we can give you another solution .

  • That’s right, the onClick function is in Activity Parametro and I’m trying to use it in Maiactivity

  • just call the function implicitly : new Parametro().onClick() (do not forget to put the method as Static)

No answers

Browser other questions tagged

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