在Bundle.getString默认值(字符串键) [英] Default value on Bundle.getString(String key)
本文介绍了在Bundle.getString默认值(字符串键)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我刚刚注意到,尽管多数干将从捆绑
包含有一个默认值,的possibiliy的情况下,关键不在于特定的存在包实例,的getString
没有这种可能性,返回null如果那样的话。
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");
谢谢!
推荐答案
您得自己把它包:
public String getBundleString(Bundle b, String key, String def)
{
String value = b.getString(key);
if (value == null)
value = def;
return value;
}
这篇关于在Bundle.getString默认值(字符串键)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文