When starting service i need to check for particular setting ( boolean ). Please check following code:
XML Code:
<CheckBoxPreference android:title="Enable Sleep" android:defaultValue="false" android:key="checkbox_preference"/>
Java Code:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
boolean sleepEnabled = prefs.getBoolean("checkbox_preference", true);
if(sleepEnabled) {
// Code
}
Even tho, default value in XML is "false" i got "true" on every service start. I know this is because I'm setting parameter in "getBoolean()" method to "true", but I need there the actual value of checkbox ..
Why Am I getting always "true" when starting service?
p.s If i go to Settings and change the value of checkbox it's fine. Then when starting service again it pulls the actual value. So how to get the actual DEFAULT value set in XML?
change this:
boolean sleepEnabled = prefs.getBoolean("checkbox_preference", true);
to this:
boolean sleepEnabled = prefs.getBoolean("checkbox_preference", false);
and take a look at the documentation:
http://developer.android.com/reference/android/preference/Preference.html#attr_android:defaultValue
Related
I am combining wicket and jQuery for some project.
I have in HTML:
<a wicket:id="link" testAttr="test"></a>
And using jQuery I modify this attribute when other components on the page are clicked. My question here is how to obtain the current value of attribute "testAttr" from Java? I am fetching the value on every ajax call and see with inspect element that is changed, so no problem with that.
I have tried with getMarkupAttributes() but I always get value "test" and not the current one which I see on the page with inspect element. Also tried with AttributeModifier and Appender, onComponentTag, but had no luck.
Does anybody have an idea what to do here?
You have to send the current attribute value to the server as a 'dynamic extra parameter':
link.add(new AjaxEventBehavior("click") {
updateAjaxAttributes(ARA ara) {
super.updateAttributes(ara);
ara.getDynamicExtraParameters()
.add("return {'q' : jQuery('#' + attrs.c).attr('testAttr') };");
}
onEvent(ART art) {
RequestCycle requestCycle = RequestCycle.get();
String val = requestCycle.getRequest()
.getRequestParameters()
.getParameterValue("q")
.toString();
// ...
}
});
I'm trying to retrieve a default value:
Definition:
<integer name="keyOfDefaultValue">2</integer>
Referenced as:
android:defaultValue="#integer/keyOfDefaultValue"
Actual code:
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
int intVar = sharedPrefs.getInt(keyOfDefaultValue, fallbackIntValue);
I get ClassCastExceptions stating that I'm trying to cast String to an Integer. I've read the Android docs about default values:
http://developer.android.com/reference/android/preference/Preference.html#attr_android:defaultValue
so I expected to work with the correct type, but apparently I get a String.
Can someone confirm that default values defined in .xml are always to be retrieved as Strings? Or you could just point me to a page in the documentation where this is explained...
Thanks
if you check the return value on AOSP code for different types of preference
EditTextPreference, ListPreference return String values
SwitchPreference,CheckBoxPreference returns boolean
MultiSelectListPreference returns a set of strings
If you want to change it, I guess you can always override
onGetDefaultValue in your customPreference , if you have one
How I can programmatically get the default value of a ListPreference as it have defined in the XML?
Here is the snippet of my ListPreference:
<ListPreference
android:defaultValue="60"
android:entries="#array/interval_entries"
android:entryValues="#array/interval_values"
android:key="interval"
android:summary="#string/interval_summary"
android:title="#string/interval_title" />
I have been through the docs but I have not found a way to get this. Maybe I have overlooked it.
For PreferenceActivity (deprecated with Fragment), try:
ListPreference lp = (ListPreference) this.findPreference(this.getString(R.string.my_key));
lp.getValue();
Where my_key is the key value assigned to this ListPreference. Note: This value is defined in strings.xml. If you have hard-coded your key with a literal string, then substitute my_key with whichever string you've given for the android:key tag. So, in your case, the codes will be:
ListPreference lp = (ListPreference) this.findPreference("interval");
lp.getValue();
Are you trying to initially set the default value or reset it to default? Regardless have a look at:
PreferenceManager.setDefaultValues(this, R.xml.your_pref_xml, false);
and the documentation
I need to pass a parameter to my page and I can't find a way to pass parameters that might be null.
If I do:
PageParameters pageParameters = new PageParameters ();
pageParameters.add ("key", null);
This will result in an exception
java.lang.IllegalArgumentException: Argument 'value' may not be null.
at org.apache.wicket.util.lang.Args.notNull(Args.java:41)
If I use Google Guava's Optional, I can't find any way to cast the object even if the the Optional object is not holding a null ie not equals to Optional.absent() :
In my landing page's constructor I do
StringValue sv = parameters.get ("key");
sv.to ( Optional.of (MyEnum.SOME_ENUM_CONSTANT).getClass () );
and when I run it I get this error:
org.apache.wicket.util.string.StringValueConversionException: Cannot
convert 'Optional.of(SOME_ENUM_CONSTANT)'to type class
com.google.common.base.Present.
Am I doing something wrong?
Is there any other way to pass a possibly null object in wicket 6?
I noticed in wicket 1.4 they have PageParameters.NULL which seems to have dissapeared in wicket 6.
Thank you
This might be too simple, but what's wrong with
Object value = ?
if (value != null) {
pageParameters.add ("key", value);
}
and
StringValue sv = pageParameters.get("key");
if (!sv.isNull()) {
// process string value
}
All page parameters in wicket will be treated as Strings eventually. The idea is that a page parameter will be on the URL of the request.
From the javadoc:
Suppose we mounted a page on /user and the following url was accessed /user/profile/bob?action=view&redirect=false. In this example profile and bob are indexed parameters with respective indexes 0 and 1. action and redirect are named parameters.
If you add something like x=y&x, the parameter x will appear twice, once with the String y and another with the empty string.
Depending on what you are trying to accomplish I would suggest to either
Don't pass the parameter at all when there is a null value required and use the isNull or toOptional methods.
Use indexed parameters and test for presence of a certain word
I'm trying to write some code that will reference a bool.xml file and will reference the current value inside the bool.
<bool name="enableQAurl">true</bool>
With this I want to be able to reference this in code, so that if it's set to True it does something and if false does something else. Just a simple If and else statement.
Any code references or feedback is greatly appreciated.
Resources res = getResources();
boolean enableQAurl = res.getBoolean(R.bool.enableQAurl);
Source:
http://developer.android.com/guide/topics/resources/more-resources.html
The above answer of kaderud's will work perfectly. If you are not in Activity, you have to use your context.
If you are in fragment or adapter then you have to follow below.
boolean enableQAurl = context.getResources().getBoolean(R.bool.enableQAurl);