Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default value on Bundle.getString(String key)

Tags:

android

bundle

I've just noticed that, while most of getters from a Bundle have the possibiliy of including a default value, in case the key doesn't exist in that particular bundle instance, getString does not have that possibility, returning null if that case.

Any ideas on why is that and if there is some way of easy solution to that (by easy I mean not having to check each individual value or extending the Bundle class).

As an example, right now you have only this:

bundle.getString("ITEM_TITLE");

While I would like to do:

bundle.getString("ITEM_TITLE","Unknown Title");

Thanks!

like image 410
Víctor Penela Avatar asked Feb 08 '11 11:02

Víctor Penela


1 Answers

Trojanfoe has the best solution if that is what you want, though once you get into dealing with defaults for other datatypes you'll have to do the same thing for them all.

Another solution would be to check to see if the bundle contains the key:

String myString = bundle.containsKey("key") ? bundle.getString("key") : "default";

It's not as nice as a function, but you could always wrap it if you wanted.

like image 171
csaunders Avatar answered Nov 14 '22 07:11

csaunders