-1

I create ImageViewProduct class which is extending ImageView class so i can store some variables in my ImageViewProduct object. But i can't get the ImageViewProduct's variable inside setOnClickListener method..

ImageViewProduct class :

public class ImageViewProduct extends ImageView {
    boolean toUpload = false;

    public ImageViewProduct(Context context) {
        super(context);
    }

    public void setToUpload(boolean toUpload) {
        this.toUpload = toUpload;
    }

    public boolean isToUpload() {
        return toUpload;
    }
}

MainActivity class :

ImageViewProduct ivProduct = new ImageViewProduct(this);
ivProduct.setToUpload(true)
ivProduct.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(final View v) {
        Log.i("zihad", "is to upload : "+v.isToUpload());
    }
};

The log inside onClickListener is error cannot resolve method 'isToUpload()'. I've try to change the onClick(final View v) to onClick(final ImageViewProduct v) but error. How to fix it?

zihadrizkyef
  • 1,849
  • 3
  • 23
  • 46

2 Answers2

1

If you only ever apply this to an ImageViewProduct then you could cast v to an ImageViewProduct.

public void onClick(final View v) {
    ImageViewProduct productView = (ImageViewProduct) v;
    Log.i("zihad", "is to upload : "+productView.isToUpload());
}

More info on casting at this QnA.

Community
  • 1
  • 1
musicin3d
  • 1,028
  • 1
  • 12
  • 22
0

Change

Log.i("zihad", "is to upload : "+v.isToUpload());

to

Log.i("zihad", "is to upload : "+ivProduct .isToUpload());

v is the variable of View. isToUpload is a method of ivProduct class

jace
  • 1,634
  • 3
  • 14
  • 41