Arrays are serializable in Java so why not just store the array since
you said its always going to be String or ints.


                Object[] foo = new Object[10];
                foo[0] = new Integer(7);
                foo[6] = "Hello";

                Intent intent = new Intent(this, HomeActivity.class);
                intent.putExtra("foo", foo);

On Aug 5, 3:53 pm, Herb Jellinek <[email protected]> wrote:
> That's an old trick that can introduce big problems.  You must ensure
> that the delimiter doesn't appear in any of the items, and if that's
> not possible, you (1) can't use StringTokenizer to parse the string,
> and (2) have to invent some kind of quotation mechanism to protect the
> delimiter characters that appear in the data, and parse it "by hand."
>
> The approach I'm using is simple.  It takes advantage of the fact that
> ArrayList is Serializable:
>
>     ArrayList<String> strings = new ArrayList<String>(foo.length);
>     for (Object elt : foo) {
>             strings.add(elt.toString());
>     }
>
>     Intent broadcastIntent = new Intent(MY_INTENT);
>     broadcastIntent.putExtra(MY_EXTRA, strings);
>
> Though still not quite satisfactory, because I don't like having to
> turn the Integer elements into Strings.
>
>         Herb
>
> On Aug 3, 9:42 pm, Sarwar Erfan <[email protected]> wrote:
>
>
>
> > Hi,
> > You can try concatenating the foo's into a single string delimited by
> > some character that will NOT appear in any of the strings. For
> > example, I am assuming pipe character (|) will never appear in any of
> > the strings you want to pass.
> > Then, create a string like:
>
> > "7|Hello|This is another string|1123|"
> > Put this string as extra. In the receiving part, tokenize the string
> > by |, then you get all the elements.
>
> > Regards
> > Sarwar Erfan
>
> > On Aug 4, 2:02 am, HerbJellinek<[email protected]> wrote:
>
> > > I have a program that sends a broadcast Intent that needs an "extra"
> > > value.  The value is declared as being of type Object[], though I know
> > > that the elements are always of type Integer or String.  That's part
> > > of an API not under my control.
>
> > > What's the most concise and/or efficient way to do the equivalent of
> > > the putExtra call below?  I am willing to convert foo to another data
> > > type for the putExrta and convert back in the receiver(s).
>
> > >     Object[] foo = new Object[FOO_SIZE];
> > >     ...
> > >     foo[0] = new Integer(7);
> > >     ...
> > >     foo[6] = "Hello";
>
> > >     Intent broadcastIntent = new Intent(MY_INTENT);
> > >     // assert: all elements of foo are Integer or String
> > >     broadcastIntent.putExtra(MY_EXTRA, foo); // XXX not correct
>
> > > Thanks.

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to