Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/185.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何知道选择了哪个imageview_Android_Android Intent_Android Imageview - Fatal编程技术网

Android 如何知道选择了哪个imageview

Android 如何知道选择了哪个imageview,android,android-intent,android-imageview,Android,Android Intent,Android Imageview,我使用循环创建了三个ImageView。我已经在活动中定义了变量pictureSelected,以跟踪单击哪个ImageView。奇怪的是,在onActivityResult中,无论单击哪个ImageView,pictureSelected变量值都重置为0。接下来,我将选中的图片存储到一个额外的目的中,但结果是相同的 这是我的密码: (这来自onCreate中调用的函数initView) 您可以通过setTag()完成此操作 下面是一个例子: for(int i=0;i<3;i++){

我使用循环创建了三个ImageView。我已经在活动中定义了变量pictureSelected,以跟踪单击哪个ImageView。奇怪的是,在onActivityResult中,无论单击哪个ImageView,pictureSelected变量值都重置为0。接下来,我将选中的图片存储到一个额外的目的中,但结果是相同的

这是我的密码: (这来自onCreate中调用的函数initView)

您可以通过setTag()完成此操作

下面是一个例子:

for(int i=0;i<3;i++){
            ImageView temp = new ImageView(this);
            String url = "/images/"+user.getEmail()+"_"+i+".jpg";
            new ImageLoadTask(this,Constants.SERVER_URL+url,temp).execute();
            int width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, getResources().getDisplayMetrics());
            int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, getResources().getDisplayMetrics());
            temp.setLayoutParams(new ViewGroup.LayoutParams(width, height));
            temp.setTag(url);
            temp.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String selectedPictureUrl = v.getTag();
                    Intent intent = new Intent();
                    intent.putExtra("selected",selectedPictureUrl);
                    intent.setType("image/*");
                    intent.setAction(Intent.ACTION_GET_CONTENT);
                    intent.addCategory(Intent.CATEGORY_OPENABLE);
                    startActivityForResult(Intent.createChooser(intent, "Select Picture"), 1);

                }
            });
            LinearLayout linearLayout = (LinearLayout) findViewById(R.id.layoutProfilePictures);
            linearLayout.addView(temp);
            profilePictures.add(temp);
        }

for(inti=0;iLog.d是否也打印出0

在这里的示例代码中,您从未在onActivityResult方法中实际设置pictureSelected变量。如果在代码中设置的是相同的,则可以解释您的问题。

使用此选项

startActivityForResult(Intent.createChooser(intent, "Select Picture"), finalI);
在活动结果中

if(requestCode==0)
{
    //for first imageview
}
else if(requestCode==1)
{
    //for second imageview
}
else if(requestCode==2)
{
    //for third imageview
}

我如何检索活动结果的意图?使用请求代码是一个好主意,但执行if-then-else则不是(他可以像使用pictureSelected一样使用请求代码)。
startActivityForResult(Intent.createChooser(intent, "Select Picture"), finalI);
if(requestCode==0)
{
    //for first imageview
}
else if(requestCode==1)
{
    //for second imageview
}
else if(requestCode==2)
{
    //for third imageview
}