How do I call a method that is in another class on onclick

Asked

Viewed 316 times

-1

Guys I have a simple doubt basically, but I’m traveling on ideas.

I have a class called chamada:

@Override
public boolean onLongPressClickListener(View view, int position) {
    // opcional, utilize o valor que achar melhor
    int **tamanhoPadraoCompartilhamento** = 395; 

    String imgPath = DataUrl.getUrlCustom(mList.get(position)
                  .getUrlPhoto(), tamanhoPadraoCompartilhamento);

    Log.i("log", "Path img em Server: " + imgPath);

    picassoDownloadImg(imgPath);

    return true;
}

but I’m already in another class I just want to call the method. (tamanhoPadraoCompartilhamento) I think that’s it. Here follows the class I’m trying to do the method invocation.

@Override
public void onClick(View v) {

    //campo onde vou chamar o método da outra class

}

Good as I do to call the method.


i have a class with a certain function to share an image by pressing a banner in the application.

So, I’m going to put that same function on button in a cardView.

this code is in a class with the name Carfragment.

/*
    MÉTODO QUE COMPARTILHAR O BANNER.
 */
@Override
public boolean onLongPressClickListener(View view, int position) {


    int tamanhoPadraoCompartilhamento = 395; // opcional, utilize o valor que achar melhor
    String imgPath = DataUrl.getUrlCustom(mList.get(position).getUrlPhoto(), tamanhoPadraoCompartilhamento);
    Log.i("log", "Path img em Server: " + imgPath);

    picassoDownloadImg(imgPath);
    return true;
}

private void picassoDownloadImg(String imgPath) {
    Picasso.with(getActivity())
            .load(imgPath)
            .into(new Target() {
                      @Override
                      public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                          try {
                              String root = Environment.getExternalStorageDirectory().toString();
                              File myDir = new File(root + "/partiuapp");
                              boolean success = true;

                              // CRIANDO DIRETÓRIO CASO NÃO EXISTA
                              if (!myDir.exists()) {
                                  success = myDir.mkdirs();
                              }

                              // CLÁUSULA DE GUARDA
                              if (!success) {
                                  return;
                              }

                              String name = "shared_image_" + System.currentTimeMillis() + ".jpg";
                              myDir = new File(myDir, name);
                              FileOutputStream out = new FileOutputStream(myDir);
                              bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);

                              out.flush();
                              out.close();
                              shareEventImg(name); // CHAMA O CÓDIGO INTENT PARA COMPARTILHAR A IMG
                          } catch (Exception e) {
                              e.printStackTrace();
                          }
                      }

                      @Override
                      public void onBitmapFailed(Drawable errorDrawable) {
                      }

                      @Override
                      public void onPrepareLoad(Drawable placeHolderDrawable) {
                      }
                  }
            );
}

private void shareEventImg(String imgName) {

    Intent shareIntent = new Intent(Intent.ACTION_SEND);
    shareIntent.setType("image/jpg");

    shareIntent.putExtra(Intent.EXTRA_TEXT, "Melhor Aplicativo de Eventos de Maceió");
    String imagePath = Environment.getExternalStorageDirectory().toString() + "/partiuapp";
    File photoFile = new File(imagePath, imgName);

    shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(photoFile));
    startActivity(Intent.createChooser(shareIntent, "Compartilhar imagem"));
}

I have another class with the name Caradapter

where I want her to do the function of the first class that is to share, put in a button.

@Override
public void onClick(View  view) {
             Log.i("log", "passou aqui: ");

        }

FOLLOWING THE FIRST-CLASS LOGIC WHAT CAN I DO TO MAKE THE CARADAPTER CLASS CALL THE FIRST-CLASS METHOD THAT IS SHARED???

thank you all.

1 answer

2

Your question is half vague, but to execute methods of another class you have two options:

Option 1

Use a method that is declared as static, as an example:

public static double somar(double a, double b) {
  return a + b;
}

So you can use it just by ordering it from the class, as for example if this method was in the class Calculo:

Calculo.somar(2, 3); // Retorna 5

Option 2 (Which seems to me to be your case)

You must have an instance of the class in which the method is, which would look something like the following:

tela1.tamanhoPadraoCompartilhamento();
  • OK Sorack I’ll validate.

  • because the method has to be Static?

  • Because when you create the method as static it becomes class method and requires no instance to run

  • @Italorodrigo take a look at this question here: Static blocks, heritage and constructors in Java. It’s not 100% what you asked but you can get an idea

Browser other questions tagged

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