1

I am currently working on an android project and I want to be able to startActivityForResult so that I can return an array of.

The array is an ArrayList<Spanned> lets say its called myArray.

From what I've read I can't return an array directly from the activty using the set result so I was thinking that once the array has added all the data to the array, I can then call the toString function on it, i.e. myArray.toString().

If I do this, I have no idea how I can then convert this back into the original ArrayList<Spanned>.

Thanks for any help you can provide.

1 Answer 1

2

Use setResult(int, Intent). From the child activity:

Intent intent = new Intent();
intent.putExtra("mydata", mydata);

setResult(RESULT_OK, intent);

Serialization

if the problem is about serialization you can use Html.toHtml and Html.fromHtml. With toHtml you conver the Spanned to string, and fromHtml you get the spanned back. In the child activity serialize the Spanned and put in extra:

Intent intent = new Intent();
ArrayList<Spanned> myData;
ArrayList<String> strings = new ArrayList<String>();
for(Spanned item : myData)
    strings.add(Html.toHtml(item));
intent.putStringArrayListExtra("mydata", strings);

setResult(RESULT_OK, intent);

to get back the data in the caller activity, use the intent passed in onActivityResult:

protected void onActivityResult (int requestCode, int resultCode, Intent data) {
    ArrayList<Strings> strings = data.getStringArrayListExtra("mydata");
    ArrayList<Spanned> spanned = new ArrayList<Spanned>();
    for(String s : strings)
       spanned.add(Html.fromHtml(s));
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your help, how would I then get this ArrayList<Spanned> back out again so that I can process the data within in.
I'm not sure if I'm missing something but still not sure what you mean with Html.toHtml etc. I have an ArrayList<Spanned> which I am setting as, in your, example, myData and storing it in the intent to set the result. In the OnActivityResult function I then need to get out my data back to being an ArrayList<Spanned>
I added an example in the answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.