lundi 5 octobre 2015

Initialize an sqlite table through an array contained in a xml resource

I have to initialize a sqllite database in android. Here is the structure of the table:

    public static abstract class FeedEntry implements BaseColumns {
    public static final String TABLE_NAME = "entry";
    public static final String COLUMN_NAME_ENTRY_ID = "entryid";
    public static final String COLUMN_NAME_TITLE = "title";
}

I have an array.xml file like the following:

<?xml version="1.0" encoding="utf-8"?>

<string-array name="my_array">

    <item>S1 999</item>
    <item>S1 10</item>
    <item>S1 111</item>
    <item>S1 101</item>

</string-array>

Now I want to take the items in this resource, split them into two and use the result to initialize my database. I have a function which should do the trick:

    public void onCreate(SQLiteDatabase db) {
    db.execSQL(SQL_CREATE_ENTRIES);

    ContentValues values = new ContentValues();
    Resources res = fContext.getResources();
    String[] myArray = res.getStringArray(R.array.my_array);
    for (String item : myArray){
        System.out.println(item);
        String[] split = item.split(" ");
        values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_ENTRY_ID, split[1]);
        values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE, split[0]);
        db.insert(FeedReaderContract.FeedEntry.TABLE_NAME, null, values);
    }

The problem is that instead of getting the expected result

S1 999 
S1 10 
S1 111 
S1 101

I get the following:

1 1
2 1
3 1
4 1

In fact, each item printed by System.out.println(item); consists of a pair of two value, like 1 1, 2 1 and so on. How should I modify the java method onCreate to get the correct couple of strings, like S1 999?

Aucun commentaire:

Enregistrer un commentaire