我创建了ImageViewProduct类,它扩展了ImageView类,因此我可以在ImageViewProduct对象中存储一些变量。但我无法在setOnClickListener方法中获取ImageViewProduct的变量..
ImageViewProduct类:
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类:
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());
}
};
onClickListener中的日志是错误cannot resolve method 'isToUpload()'
。我尝试将onClick(final View v)
更改为onClick(final ImageViewProduct v)
,但错误。如何解决?
答案 0 :(得分:1)
如果您只将其应用于ImageViewProduct
,那么您可以将v
投射到ImageViewProduct
。
public void onClick(final View v) {
ImageViewProduct productView = (ImageViewProduct) v;
Log.i("zihad", "is to upload : "+productView.isToUpload());
}
答案 1 :(得分:0)
更改
Log.i("zihad", "is to upload : "+v.isToUpload());
到
Log.i("zihad", "is to upload : "+ivProduct .isToUpload());
v是View的变量。 isToUpload是ivProduct类的方法