0

I have a Listview and TakePicture button event for each list item. I need to send position and get position back on OnActivityResult method to Update Image to List Item using that position

My code is

holder.TakePicture1.Click += delegate (object sender, System.EventArgs args)
  {
     if (remnantModel != null)
        {
           Intent camIntent = new Intent(MediaStore.ActionImageCapture);
           camIntent.PutExtra("Position", position);
           context.StartActivityForResult(camIntent, 2);
         }
  };

In OnActivityResult function

protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
  {
       base.OnActivityResult(requestCode, resultCode, data);

       switch (requestCode)
        {
        case 2:
            if (data != null)
            {
                Bundle extras = data.Extras;
                Bitmap imageBitmap = (Bitmap)extras.Get("data");
                int pos = data.GetIntExtra("position", 0);
                int position = data.Extras.GetInt("position", 0); 
                // Not getting position value here
                RemList[pos].Path.SetImageBitmap(imageBitmap); 
            }
        }
  }

As above code, I am using native ActionImageCapture event and not able to get Position in OnActivityResult function.

How can I get data i.e position in OnActivityResult method?

halfer
  • 19,824
  • 17
  • 99
  • 186
kumar Sudheer
  • 705
  • 3
  • 8
  • 28
  • Hello, have you checked my answer? – Robbit Jun 11 '18 at 08:02
  • @JoeLv-MSFT thank you for your feedback. yeah I worked on using static varilable, I am not getting accurate position when hit click event in a list. – kumar Sudheer Jun 11 '18 at 09:15
  • That means when you click item 2, it will show position 4? If so, I guess you need handle the reuse problem. You can refer to [this](https://stackoverflow.com/questions/20541821/get-listview-item-position-on-button-click), use Tag to handle the reuse problem. – Robbit Jun 11 '18 at 09:19
  • Hi, have you solved it? – Robbit Jun 12 '18 at 07:37
  • Yes, I make it happen by passing position to static variable on click event and by seeing getview method by not calling multiple times – kumar Sudheer Jun 12 '18 at 13:21

1 Answers1

0

get data i.e position in OnActivityResult method

You can't get the position in OnActivityResult by your code.

1) it should be Position, not position.

2) if you change it to Position (data.GetIntExtra("Position", 0);), you still can't get the position.

About StartActivityForResult method:

If you want to jump from AcitivityA to AcitivityB without return value, you can use StartActivity, but if you want to get data from AcitivityB, you need to use StartActivityForResult, and at the same time, you need to use SetResult method in the SystemCameraAcitvity, there is a problem, we can't handle the SystemCameraAcitvity, it is system activity, we don't have the Activity in our application, so we can't use SetResult to send the position back to our activity which contains the ListView.

So, the solution is that, you need to define a static variable in your activity to store the position, change its value when you click the button, and after taking the picture, you need to judge the resultCode and requestCode, and then, use the static variable to set the picture.

Robbit
  • 4,300
  • 1
  • 13
  • 29