获取的ImageView的ID [英] Getting id of imageview

查看:351
本文介绍了获取的ImageView的ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个循环使得一些imageViews的编程并赋予它们的ID。但是,当在OnClick方法我需要得到图像的ID,它返回只有最后一个图像的ID。如何获取点击该图像的id?

在code我使用的:

 为(INT J = 0; J< 5; J ++){
    TR的TableRow =新的TableRow(本);
    图片网址=htt​​p://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.

这篇关于获取的ImageView的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆