4

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 :)

mnille
  • 1,328
  • 4
  • 16
  • 20
DISSENNATORE
  • 81
  • 1
  • 14

3 Answers3

4

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();
}
babadaba
  • 814
  • 5
  • 20
  • Oh, I've made the array directly in the Java file, so here's why it gave the same errors, thank you very much, this will run for sure :D – DISSENNATORE Jul 01 '16 at 16:03
  • 1
    glad to help :) if you have any further problems with this just make a comment – babadaba Jul 01 '16 at 16:04
3

See Java "too many constants" JVM error for an explanation of why you got the error.

I would suggest trying to add the words to an array as @babadaba suggests, or put them in a map, and iterate through them.

Another solution is to read them from a file.

Community
  • 1
  • 1
Rex Wagenius
  • 1,302
  • 1
  • 12
  • 13
  • It doesn't work with an array, it's the same. Yes, I'll go with the text file, but I'll do as @danny117 said because it's better for performances in that way – DISSENNATORE Jul 01 '16 at 15:29
  • Then you have to read from a file. I'm not 100% sure but a .properties file might be convenient for you to use. – Rex Wagenius Jul 01 '16 at 15:34
1

Wow you've hit max constants. The constants are assigned a number. You've seen it R.id.xxxxxx In your case it ran out passed the maximum. Store the words in a text file resource. Read the text file and insert into the database. There are also other hacks you have to search for them to have a pre-loaded sqlite database with the app. The resource text file is far easier to maintain adding new words and such.

danny117
  • 5,581
  • 1
  • 26
  • 35