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);
Related
In the context of using the OWLAPI 4.0 API, this following line of code:
ontologyIRI = IRI.create(o.getOntologyID().getOntologyIRI().toString());
returns the following string :
"Optional.of(http://www.indytion.com/music/composition)".
What I need is the sole string "http://www.indytion.com/music/composition".
I tried to declare ontologyIRI as Optional and use .get() method, .orElse(), etc. to no avail. I still have the returned string that includes the 'optional.of()' part.
My question is : How could I get the internal string?
Thank you very much for your help.
Edit : The full code the method
private void LoadOntology(String ontologyPath)
{
OWLOntologyManager man = OWLManager.createOWLOntologyManager();
OWLOntology o;
File ontologyFile = new File(ontologyPath);
Optional<IRI> ontologyIRI;
try {
o = man.loadOntologyFromOntologyDocument(ontologyFile);
ontologyIRI = Optional.of(IRI.create(String.valueOf(o.getOntologyID().getOntologyIRI()).toString()));
System.out.println("Ontology IRI is: " + ontologyIRI.get());
} catch (OWLOntologyCreationException e) {
e.printStackTrace();
}
}
The System.out.println() returns exactly this string:
"Ontology IRI = Optional.of(http://www.indytion.com/music/composition)"
Use .get() instead of toString()
//Returns 'Optional[example]'
Optional.of("example").toString();
//Returns 'example'
Optional.of("example").get();
Short answer: Replace
Optional.of(IRI.create(String.valueOf(o.getOntologyID().getOntologyIRI()).toString()));
with
o.getOntologyID().getOntologyIRI().get();
Longer answer: you're doing an awful lot of back-and forth that's pointless at best and actively harmful in some cases:
In no particular order:
others have already commented that IRI instances are immutable, so creating a new one from an existing one is kind of pointless (if harmless).
calling Optional.of() if you don't intend to actually return an Optional is almost always a bad idea.
String.valueOf() is used to get a string-representation of some value and is usually most useful for debugging, but should not be relied on to fully round-trip everything about an object (the same applies to toString().
So basically what you're left with is this:
o.getOntologyID().getOntologyIRI() gives you an Optional<IRI>
you want an IRI.
Optional::get returns the value contained in the optional, if one exists, so you simply need to call get()
If, however the Optional is empty (i.e. there is no underlying value) then get() will throw a NoSuchElementException. This might or might not be what you want. To work around this either call isPresent() before calling get() to check if a value exists or use any of the multitude of other accessor methods, some of which have "built-in checks" in a way.
Finally, it seems that the problem was not the code itself. This is how the problem has been solved. But I don't understand why it has been solved :
I copy/paste (in the same file) the "shouldAddObjectPropertyAssertions()" example from OWLAPI4 examples -> This example code runs OK (but does not use the getOntologyID() method as I do).
Change SDKs to another minor version '1.8.0_61'
Change again with initial and desired SDK '1.8.0_131'
Invalidate caches and restart the IDE
Problem solved. The exactly same code :
ontologyIRI = o.getOntologyID().getOntologyIRI().get();
System.out.println("Ontology IRI is: " + ontologyIRI);
Now returns the expected string value : "http://www.indytion.com/music/composition" and not "Optional.of(http://www.indytion.com/music/composition)" anymore.
If someone can explain why it has been fixed, I would be very glad.
Thank you again for your help.
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
I defined a EditText in XML with attribute android:inputType="numberSigned", so, when I try to get it in Java Code like:
int type = mEditText.getInputType();
switch(type){
case InputType.TYPE_NUMBER_FLAG_SIGNED:
//do when I get EditText defined with 'numberSinged'
//do something
break;
}
But, It doesn't work for me. So I try to check Android source code, TYPE_NUMBER_FLAG_SIGNED=4096. When I try to print println(mEditText.getInputType()),it turns to be 4098. And I can't find any variable equals 4098.
Can anybody tell me the reason?
I'm not good at English, may you can understand me! Thanks!
there can be multiple flags assigned to inputType. To find out if a flag is set or not, use the bitwise AND (&) operator:
int type = mEditText.getInputType();
if((type & InputType.TYPE_NUMBER_FLAG_SIGNED) > 0)
{
// your stuff here
}
I guess, the usage of switch case is not possible here.
TYPE_NUMBER_FLAG_SIGNED Constant Value: 4096 (0x00001000) .
get more information here
I want to read/extract the value from HSSFComment.
I can access the HSSFComment by the following code:
HSSFComment comment = workSheet.getCellComment(1, 0);
But, how can I get the text/value from that "comment" instance?
there are tow methods in HSSFComment:
getTextObjectRecord()
getNoteRecord()
But both are protected methods...that's why I can't access those from my class. in other word, these methods are not visible from my class. Following line of code doesn't compile.
TextObjectRecord txo = comment.getTextObjectRecord();
Any comments?
Use getString() inherited from HSSFTextBox. This returns an HSSFRichTextString, which itself has a getString() method to get the plain text. In otherwords
String comment = cell.getComment().getString().getString();
Which you can't do like that due to the possibility of null returns, but that's the idea.
What I want to do is to get a specific text from strings.xml dynamically. I think it will involve to access an object variable dynamically.
There will be a function like:
public void getDynamicString(int level) {
text.setText(R.string.levelText_+level);
}
And in strings.xml there will be <string name="levelText_5">Text level 5</string>
I would rather not create a list with all the text resources. Can one do this in Java/Android.
Use the method getIdentifier(name, defType, defPackage) of the Resources class to get the id of a resource by name. Then you can do a normal getString(id) from the same class.
EDIT: a bit of Googling revealed this: this. You can find sample usage there.
Try: getResources().getString(R.id.stringId).
You should look at using getIdentifier(String, String, String) of the Resources class.
All you have to do is call
this.getString(R.string.levelText_5)
If your in an area of the program in which you have access to a Context or Application, such as a ListAdapter call:
context.getString(R.string.levelText_5)
or
application.getString(R.string.levelText_5)
if you have no access to the context or application then call:
getResources().getString(R.String.levelText_5);
To do it dynamically call:
String name = "levelText_"+level;
int id = getIdentifier(name, "string", "com.test.mypackage");
getResources().getString(id);
I had the same problem and I fixed it using this
okey , whenever you want to access a string from strings.xml dynamically and what i mean by that is to avoid using getResources().getString(R.id.stringId) ,you create a string in which you can manipulate dynamically however you want in our case uriq ("stupid variable name") and then you create resource object which is in my example level_res and initialize it then you use this method called getIdentifier() which accepts your dynamic string as a parameter ,now u simply pass your ressource to the method getstring(mysttring)
String uriq="level"+level_num;
level_res=getResources();
int mystring=getResources().getIdentifier(uriq,"string",getPackageName());
String level=level_res.getString(mystring);