Search code examples
dynamicviewonactivityresult

passing view id to onActivityResult with putExtra does not work


I dynamically add views to a LinearLayout based on field types comming from an api.

If the field type dictates 'image' i create an image selection button and add that to the view.

I also create an imageview so when onActivityResult is called i can assign the choosen image to that imageview.

However, to find the dynamcally created imageview i need to pass some sort of identifier.

Somehow the putExtra() function does NOT pass the id.

initial vars:

private static final int SELECT_SINGLE_PICTURE = 101;
public static final String IMAGE_TYPE = "image/*";
private ImageView selectedImagePreview;
protected int tmpviewid;

dynamic field creation:

LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayout2);
TextView tmltname = new TextView(NewJobActivity.this);
tmltname.setText(vwa.getName());
ll.addView(tmltname);

Iterator it = attributes.entrySet().iterator();
while (it.hasNext()) {
    if(val.getType().equals("image") ) {
        Button btn = new    Button(NewJobActivity.this);
        btn.setText(R.string.select_picture);
        ImageView iv = new ImageView(NewJobActivity.this);
        iv.setId(View.generateViewId());
        tmpviewid = iv.getId();

        btn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
                Intent intent = new Intent();
                intent.setType(IMAGE_TYPE);
                intent.setAction(Intent.ACTION_GET_CONTENT);
                intent.putExtra("imageid",  tmpviewid);

                startActivityForResult( Intent.createChooser( intent, getString(R.string.select_picture)), SELECT_SINGLE_PICTURE );
            }
        });

        ll.addView(btn);
        ll.addView(iv);
}

onActivityResult:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        if (requestCode == SELECT_SINGLE_PICTURE) {
            Uri selectedImageUri = data.getData();
            int myviewid = data.getIntExtra("myviewid",-1);
            Log.i("##MYLOG###", "###imageid:" + myviewid);
            try {
                Bitmap pic =  new  UserPicture(selectedImageUri, getContentResolver()).getBitmap();
                ImageView image = (ImageView) NewJobActivity.this.findViewById(myviewid);
                image.setImageBitmap(pic);
            }

the image selection is working and selectedImageUri is populated but why is data.getIntExtra not retrieving the passed myviewid? (it always get the default value -1 but the tmpviewid DOES get valid id's (1,2,3 etc...))

all the accepted solutions suggest that my code should work.

maybe there is something else going on?

or perhaps I am going about the wrong way?

i noticed somewhere else that get- and setTag might be also a solution.. but i still would need to pass it around some way.


Solution

  • Ok, now I got the picture ;)

    You cannot pass custom extra params, well.. you can, but the called activity will not use it. When return to the caller it will send just the activity's available extras result...

    If you setted up your Activity as SingleInstance in Manifest, it will not be recreated on result so you can add a public class variable "public static int referImage = 0" and set it on button click with the clicked ID. Then when you got the result you will have the image id reference on this variable.

     btn.setOnClickListener(new View.OnClickListener() {
                public void onClick(View arg0) {
                referImage = tmpviewid;
                ....
    

    I am not sure if this solution works but the custom Extra cannot be retrieved this way.

    Let me know and I will try to figure another way.

    Example:

    public class myActivity extends Activity{
       public static int referImage = 0;
    
       ..... Override methods ..etc
    
       while (it.hasNext()) {
        if(val.getType().equals("image") ) {
          final int tmpviewid = iv.getId();
    
          ... more code
          btn.setOnClickListener(new View.OnClickListener() {
                public void onClick(View arg0) {
                    referImage = tmpviewid;
    
    
    }
    

    When user click on Button it will set referImage = tmpviewid;

    When Activity retur for Result the last value of referImage will be Button user clicked.

    onActivityResult you just have to read referImage value.