[android-developers] Re: Setting an Intent extra value that's an array of String and Integer

2010-08-06 Thread Herb Jellinek
Sam,
You're absolutely right.  I overlooked the fact that all arrays
are Serializable.  Since the elements are all Integer and String
Objects, which are also Serializable, I don't need to do any
conversion.  I can just pass the array itself.

>From the Java Language Spec, 3rd Edition, section 10.7:

Every array implements the interfaces Cloneable and
java.io.Serializable.

Thanks.

Herb

On Aug 5, 3:03 pm, samspade79  wrote:
> 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  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 strings = new ArrayList(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  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 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 android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en


[android-developers] Re: Setting an Intent extra value that's an array of String and Integer

2010-08-05 Thread DanH
Another option would be to convert the data to JSON.  Fairly general
for integers and strings.

On Aug 3, 3:02 pm, Herb Jellinek  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 android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en


[android-developers] Re: Setting an Intent extra value that's an array of String and Integer

2010-08-05 Thread samspade79
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  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 strings = new ArrayList(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  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 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 android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en


[android-developers] Re: Setting an Intent extra value that's an array of String and Integer

2010-08-05 Thread Herb Jellinek
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 strings = new ArrayList(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  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 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 android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en


[android-developers] Re: Setting an Intent extra value that's an array of String and Integer

2010-08-03 Thread Sarwar Erfan
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, Herb Jellinek  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 android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en