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

-1

I'm having a basically simple question, but I'm traveling in 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 want to just call the method. ( tamanhoPadraoCompartilhamento ) I think that's it. Here is the class I am trying to invoke the method.

@Override
public void onClick(View v) {

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

}

Good how do I 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 button-like function on a cardView.

This code is in a class named 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 named CarAdapter

where I want it to do the function of the first class that is to share, but on a button.

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

        }

FOLLOWING THE LOGIC OF THE FIRST CLASS WHAT CAN I DO WHAT IN THE CARADAPTER CLASS CALL THE FIRST CLASS METHOD THAT IS SHARED ???

Thank you all.

    
asked by anonymous 10.12.2016 / 07:13

1 answer

2

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

Option 1

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

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

So you can use it just by requesting it from the class, such as if this method were in the Calculo class:

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

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

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

tela1.tamanhoPadraoCompartilhamento();
    
10.12.2016 / 13:21