我动态地将53张图片添加到我的布局的ImageView
。我想为我添加的每个图像设置唯一ID,并希望在图像点击时获取该ID。我已将标签添加到我的imageview imageView.setTag(WirelessPin.arr_WirelessItems[i]);
,但onClick ProductURL = (String) imageView.getTag();
始终返回最后一张图片的标记,即图片编号53.如何解决此问题?
这是我的代码
for (int i = 0; i < WirelessPin.arr_WirelessItems.length; i++) {
url = new URL(WirelessPin.arr_WirelessItems[i].replaceAll("\\s+","%20"));
//ProductURL = WirelessPin.arr_WirelessItems[i];
Bitmap bmp = BitmapFactory.decodeStream(url
.openConnection().getInputStream());
LinearLayout layout = new LinearLayout(con);
layout.setLayoutParams(new LayoutParams(150, 110));
layout.setGravity(Gravity.CENTER);
imageView = new ImageView(con);
imageView.setLayoutParams(new LayoutParams(140, 84));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setImageBitmap(bmp);
imageView.setTag(WirelessPin.arr_WirelessItems[i]);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//imageView.se
ProductURL = (String) imageView.getTag();
CollectDenominationsForWirelessPin obj = new CollectDenominationsForWirelessPin();
obj.WirelessPinDenominations(con, ProductURL, UserId);
}
});
layout.addView(imageView);
WirelessPin.sliderProducts.addView(layout);
}
答案 0 :(得分:3)
在OnClick方法中,将代码更改为
ImageView iv = (ImageView)v;
ProductURL = (String) iv.getTag();
答案 1 :(得分:0)
尝试在ImageView
内初始化for loop
,如下所示
for (int i = 0; i < WirelessPin.arr_WirelessItems.length; i++) {
// Other Stuff
ImageView imageView = new ImageView(con);// Declare and initialize here
imageView.setLayoutParams(new LayoutParams(140, 84));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setImageBitmap(bmp);
imageView.setTag(WirelessPin.arr_WirelessItems[i]);
//// Code