简体   繁体   中英

Best way to store content?

First of all I want to thank you for even reading this. I'm a complete noob and I'm trying to learn Android Java. I've made many little "Learn Apps" in which I just try to learn things. Today I want to make another one.

I want to have ListView in the MainActivity which contains names of Food like:

  1. Pizza
  2. Ice Cream
  3. Spaghetti
  4. Apple
  5. Orange
  6. ...

When the user clicks on one of those a SecondActivity gets called and this SecondActivity contains a two TextViews. One with the name on which the user clicked and one with a description. Example:

User clicks on Pizza.

SecondActivity looks like this:

Pizza

Pizza is blah blah blah

and if the user clicks Orange:

Orange

Oranges are fruits blah blah blah

I hope you undestand me.

My question is now. Which is the easiest way to store this descriptions? An SQLDatabse? Or should I just write them like Strings and say. If user clicks this parse this String to TextView1 and this String to TextView2? I hope you understand me and can help me! Thank you very much!!

If they are static Strings (that you may or may not want to translate later on) you would probably be best saving them as a String array in a resource.xml file:
(The filename can be anything_you_like .xml)

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="food_names">
        <item>apple</item>
        <item>orange</item>
        <item>pizza</item>
        <item>blah</item>
        <item>blah</item>
    </string-array>
    <string-array name="food_descriptions">
        <item>green, doctor friendly</item>
        <item>round, hurts eyes</item>
        <item>flat, cheezy, liked by turtles</item>
        <item>blah</item>
        <item>blah</item>
    </string-array>
</resources>

You need to locate the file inside your values folder and you then get the option to do all the fancy folder variations like values-v14 , values-v16 and values-en , values-es , values-fr and so on, and so on...

You can later access this String[] from your code to grab the appropriate version for the user's API and language with:

String[] foodNames = context.getResources().getStringArray(R.array.food_names);
String[] foodDescriptions = context.getResources().getStringArray(R.array.food_descriptions);

static final String FOOD_FORMAT = "%s :: $s";
for (int i=0; i<foodNames.length; i++) {
    System.out.println(
            String.format(FOOD_FORMAT,
                          foodNames[i],
                          foodDescriptions[i]
            )
    );
}

indivisible's answer is acceptable to your case. But considering that your content may change and may be more detailed, you should stick to an SQLite database. You only will need a simple table insert your data. Having an if else chain is not the cleanest way and definitely is not friendly when you think about scalability. But if you really want to use the second approach, use a switch, instead of an if else chain.

If in the future, you want to add other elements to your data, just change your database as you like and that is it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM