Search code examples
javaandroidandroid-studiojavac

code too large and too many constants errors (Android Studio)


I've got a problem that I cannot resolve; I searched in web, but I didn't find a precise solution, or at least I didn't understand very well, I don't know.

Anyway, I've got Android Studio v. 2.1, and here's what I'm trying to do:

At first I created an SQLite Database overriding the onCreate method, than, in another override of the same method, I wrote a method that checks if the database is empty; if true, it'll add around 300000 words. Here's this part:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Database db = new Database(getApplicationContext());
    db.open();


    if (db.queryTutteParole().getCount() == 0) {

        db.inserisciParole("pane", "no");
        db.inserisciParole("latte", "no");
        db.inserisciParole("uovo", "no");
        db.inserisciParole("farina", "no");
        db.inserisciParole("sale", "no");
        //and all the remaining words

    }

    db.close();
}

To clarify: queryTutteParole() is a method that returns a cursor of all that's in the database, and inserisciParole() puts the words with their value in the database.

Well, no errors with the code (I tested with only 5 words put), but if I put all the 300000+ words and I try to run it on my device, I have these errors:

Here's the image with the errors

What I should do?

Thanks :)


Solution

  • Create a array.xml file in your res/values folder and put your contents in it like this:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <string-array name="yourArray">
            <item>pane,no</item>
            <item>latte,no</item>
            <item>uovo,no</item>
        </string-array>
    </resources>
    

    then iterate through it like this:

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        String[] yourArray = getResources().getStringArray(R.array.yourArray);
    
    
        if (db.queryTutteParole().getCount() == 0) {
    
            for (String s : yourArray) {
                String[] splittedArray = s.split(",");
                db.inserisciParole(splittedArray[0], splittedArray[1]);
            }
        }
    
        db.close();
    }