I want to make something that searches through an XML file, to see if a string, entered by the user, exists. My XML file looks like this:
<?xml version="1.0" encoding="utf-8"?>
<resources><string-array name="dict_nl_array" ><item>ADHD</item><item>ADSL</item><item>AMvB</item><item>AOV</item><item>AOW</item><item>uniteit</item></string-array></resources>
The only difference is that my XML is about 500 times as long as this.
I tried loading the arraylist (xml) directly, but then I get this error:
Failed adding to JNI local ref table (has 512 entries)
Which seems logical, because its a gigantic list of items.
So, I was thinking, can I search through that XML file, without having to load it completely?
Ofcourse, other suggestions to do this are welcome! (If possible, with an example, I'd greatly appreciate that!)
Late answer, but it might help anyone else with this problem!
Based on this question, you can avoid this error by encoding the resource as a string, rather than a string-array.
<string name="array">ADHD#ADSL#AOV#AOW</string>
The # character separates the entries. To speed up the reformatting, just Find/Replace </item><item> with #. Then you can parse the string into an array like so:
myStringArray = getResources().getString(R.id.array).split("#");
Hope this helps!
Related
Say I have a long list like the following:
العربية
مصرى
Asturianu
Azərbaycanca
Žemaitėška
......
How can I convert it to a string-array in xml format?
<item>العربية</item>
<item>مصرى</item>
<item>Asturianu</item>
I actually could not find anything with online search relating xml formats. but I finally came up with this solution.
I copy the list to excel and arrange the in 3 cells and copy-paste to xml. Then and cut paste again all to a new single cell in excel. Replace all " "(single space without "" created for no reason) with ""(just blank without "")
It works, I hope I have not taken the time of too many people
I'm trying to store a fully qualified url, with also query params:
www.miosito.net?prova®=bis
but it's causing a problem because ® is similar to ® entity and android tell me that and html entity is not well written.
I need this because every locale uses a fully different set of url query param.
I tried with [[CDATA[.. ]] but this syntax disliked by xml parser.
The problem is not with &req but with & itself. For XML/HTML you would have to use & entity (or &), but for URLs you should rather URL-encode (see docs) strings, and in that case said & should be replaced with %26. So your final string should look like:
www.miosito.net?prova%26reg=bis
Store it like this:
<string name="my_url">"www.miosito.net?prova®=bis"</string>
Where & is the XML equivelant of the ampersand symbol &.
Percent encoding may do the trick: http://en.wikipedia.org/wiki/Percent-encoding
You'll basically have something like this: www.miosito.net?prova%26reg=bis
You can enclose your url in double quotes, something like :
<string name="my_url">"www.miosito.net?prova®=bis"</string>
This is a recommended way to enclose string resources in Android.
Update 1 : Have a look at the following link for more info :
http://developer.android.com/guide/topics/resources/string-resource.html#FormattingAndStyling
Update 2:
#WebnetMobile.com : Correct, indeed :)
'&' is being treated a special character by xml and enclosing in quotes doesn't work. I tried out
www.miosito.net?prova%26reg=bis
and it didn't work out either. I even tried enclosing it in quotes but still didn't work. Am I missing something ?
Meanwhile, the following does work :
<string name="my_url">www.miosito.net%1$sprova%2$sreg=bis</string>
and then in code :
Resources resources=getResources();
String url=String.format(resources.getString(R.string.my_url),"?","&") ;
The '%1$s' and '%2$s' are format specifiers, much like what is used in printf in C. '%1$s' is for strings, '%2$d' is for decimal numbers and so on.
can anybody help me ?? I want to add String value through coding in string.xml
I am doing this.
String name = getResources().getString(R.string.name);
if(name.lenght() < 1 ){
// getResources().setString(R.string.name);??????????????????????
}
My string.xml is
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="name"></string>
</resources>
does anybody know how i can add value of name in string.xml though coding.
Thank you!!
The resources are pretty much set in stone, so you can't modify them at runtime. If you need to store some new strings, use SharedPreferences or SQLite.
I don't think you want to do that. If you are trying to store a value in a persistent way, take a look at SharedPrefences. Google has a good introduction to it here.
It is not possible to modify the resources of an APK during runtime.
You can't edit those resources directly. You might want to look into sharedpreferences http://developer.android.com/reference/android/content/SharedPreferences.html or creating your own xml file.
You cant edit a resource or add a resource once the code is compiled. I dont know exactly what setResource does, but once your program is compiled, android builds the gen files which designate a certain amount of space for those variables, changing the variable once written would cause overflow or outofbounds errors with memory. If you want persistent values try using the SharedPrefs, SQL or even your own XML stored within the directory of the app, which you could set to only be readable by your app.
Hey, I have a lot of Strings that I use into my app, the .txt file that I use has ~14000 lines.. and each 3-10 lines are divided into sections like <String="Chapter I"> ... </String> ..
Speaking of performance/speed, should I put the sections into a Database, Or read line by line through the .txt file and check if the section number is the current one? Will this affect speed/performance?
I could also divide each ~2000 lines into a different .txt file so there would be less lines to go through. Is this a bad way of storing data? Thanks
I think sqlite would do the trick. It will probably be way faster than parsing a text file, plus you wont have to maintain the headache of your own ad hoc text database, or build a parser in the first place. Basically, use it, its way easier.
The standard way to deal with Strings in Android is to put them into res/values/strings.xml (I'm pretty sure you can have multiple String files in that directory if you like). If you are developing in Eclipse it will automatically populate the R class (the resource class) with constants that you can use to reference these Strings in your code:
R.string.mystring
Or in XML layouts:
#string/mystring
Or if you're doing something more custom you can use:
String string = getString(R.string.hello);
I would definitely choose this over a .txt file. It's much easier. All the work is done for you! Have a read of this Android article about it.
This is what a database is for. Use it.
I want to use the concept of array in my Android Application, I don't know how to do that actually.
So could anybody please help me how to do that on demand.
I guess you are talking about arrays in Android through the res folder.
Create an array.xml inside the /res/values folder with something like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="names_list">
<item>John</item>
<item>Peter</item>
<item>Charles</item>
</string-array>
</resources>
You can get that array on your Activity by doing:
getResources().getStringArray(R.array.names_list);
There are alot of different "array" types in java... there are actual arrays like Thorsten showed you and then there are lists, collections and hashes. Take you pick. :) A great place to start learning more about Java is the docs.
http://download.oracle.com/docs/cd/E17409_01/javase/6/docs/
http://download.oracle.com/docs/cd/E17409_01/javase/tutorial/
This defines an array of 5 strings:
String[] stringArray = new String[5];
However, I can not imagine that this is really what you're talking about...
CLARIFICATION
If you actually don't know what an array is, then my reply will give you a hint. In case you're talking about something else, this reply should indicate that you're not giving enough detail. You might as well be talking about this...