我在一个循环使得一些imageViews的编程并赋予它们的ID。但是,当在OnClick方法我需要得到图像的ID,它返回只有最后一个图像的ID。如何获取点击该图像的id?
在code我使用的:
为(INT J = 0; J< 5; J ++){ TR的TableRow =新的TableRow(本); 图片网址=http://ondamove.it/English/images/users/; 图片网址=图片网址+ listObject.get(J).getImage(); 图像=新ImageView的(本); image.setPadding(20,10,0,0); image.setId第(j + 1); tr.addView(图片); 尝试{ 新DownloadFileAsync(的形象,新的URL(图片网址)) .execute(图片网址); images.add(图片); //图像=新ImageView的[5]; } 赶上(例外五){ e.printStackTrace(); } tr.addView(图片); table.addView(TR,新TableLayout.LayoutParams( LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT)); image.setOnClickListener(clickImageListener);
以下是onclicklistener。
私人OnClickListener clickImageListener =新OnClickListener(){ 公共无效的onClick(视图v){ imageId = image.getId(); 意图fullScreenIntent =新意图(v.getContext(),FullImageActivity.class); fullScreenIntent.putExtra(ProfilePageNormalUser.class.getName(),imageId); ProfilePageNormalUser.this.startActivity(fullScreenIntent); }};
解决方案
在里面你可以调用的onClick(视图v)
方法 v.getId ()
- 这将返回你的查看被点击
的ID。希望这有助于。
I am making a number of imageViews programmatically in a loop and assigning them ids. But when in onClick method I need to get the id of images, it is returning the id of the last image only. How do I get the id of the image that is clicked?
The code I am using:
for (int j = 0; j < 5; j++) {
TableRow tr = new TableRow(this);
imageUrl = "http://ondamove.it/English/images/users/";
imageUrl = imageUrl + listObject.get(j).getImage();
image = new ImageView(this);
image.setPadding(20, 10, 0, 0);
image.setId(j+1);
tr.addView(image);
try {
new DownloadFileAsync(image, new URL(imageUrl))
.execute(imageUrl);
images.add(image);
//images = new ImageView[5];
}
catch (Exception e) {
e.printStackTrace();
}
tr.addView(image);
table.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
image.setOnClickListener(clickImageListener);
Following is the onclicklistener.
private OnClickListener clickImageListener = new OnClickListener() {
public void onClick(View v) {
imageId = image.getId();
Intent fullScreenIntent = new Intent(v.getContext(),FullImageActivity.class);
fullScreenIntent.putExtra(ProfilePageNormalUser.class.getName(),imageId);
ProfilePageNormalUser.this.startActivity(fullScreenIntent);
}
};
解决方案
Inside the onClick(View v)
method you can call v.getId()
- this will return you the ID of the View
being clicked. Hope this helps.