I want to play around with making an Android app that displays a famous quote of the day. So I will have 365 quotes and each day will display a new one. Any thoughts on how to accomplish this? Do I need 365 @strings in my string.xml
file? Is there a way to access an array of strings (or another data structure perhaps) by their index to accomplish this?
Asked
Active
Viewed 550 times
0
-
1You could just add a text-file to assets, which contains one quote per line. Or you could use JSON or XML, or even put it in a sqlite database and bundle the .db file with your app. – 323go Dec 31 '12 at 02:23
-
use database to store each quote with respect to the day – Abhishekkumar Dec 31 '12 at 02:49
3 Answers
2
If you want to use resources:
Resources res = getResources();
String[] quoteTexts = res.getStringArray(R.array.quotes_array);
quoteTexts[dayNumber]..<---- gives you your text
In resource file:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="quotes_array">
<item>I am rich</item>
<item>Quote 2</item>
<item>Quote 3</item>
....365 .... times....
</string-array>
</resources>

itadapter DKh
- 596
- 3
- 7
-
thanks for the answer. What if I have 2 or more strings for each quote, so the quote string and also an author string for example. I want to store both strings in a single index of the quoteTexts array, like a tuple or struct or something. For example: quoteTexts[0] = quote1, author1 – brno792 Dec 31 '12 at 20:18
2
You could use an SQLite DB with 2 columns
Day|Quote
1 |Your Quote day 1
2 |Your Quote day 2
etc...
You can compile your DB with an SQLite editor and then put the compiled db in your app.
If you want define a String array of 365 lines in string.xml
,you can do this adding the code
<string-array name="my_quotes_array">
<item>Live or exist</item>
<item>Quote 2</item>
<item>Quote 3</item>
<item>Quote 4</item>
<item>Quote 5</item>
....etc., one line per day....
</string-array>
in strings.xml
But I think this isn't a good solution

Silverstorm
- 15,398
- 2
- 38
- 52
0
All ways is ok, if this set of strings will not be changed in future, will be better to store it as static class. Because XML parser require some addition(much more than static class) time and resources(memory, cpu) for parsing , and SQLite require addition time, resource for create connection, etc.
But organice data in XML looks better(more logical, and more patency).
Look also on Raw resources versus SQLite database