0

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?

Here is how I read the data in the table:

String[] projection = {
            FeedReaderContract.FeedEntry._ID,
            FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE,
    };
Cursor cursor = db.query(
            FeedReaderContract.FeedEntry.TABLE_NAME,  // The table to query
            projection,                               // The columns to return
            null,                                // The columns for the WHERE clause
            null,                            // The values for the WHERE clause
            null,                                     // don't group the rows
            null,                                    // don't filter by row groups
            sortOrder 


);


String result = "";

    DatabaseUtils.dumpCursor(cursor);
    int iRow = cursor.getColumnIndex(FeedReaderContract.FeedEntry._ID);
    int iName = cursor.getColumnIndex(FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE); 
        for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()){
        result = result + cursor.getString(iRow) + " " + cursor.getString(iName) + "\n";
    }
    System.out.println(result);

: I have a cursor which sends a request to the database, and then I print the content of the cursor.

Helios83
  • 87
  • 1
  • 12

1 Answers1

0

You are splitting your string incorrectly. to split a string by space use the below code.

str = "This is the string seperated by space";
String[] splited = str.split("\\s+");
karan
  • 8,637
  • 3
  • 41
  • 78