1

I'm trying to get the data from a database on Sqlite. first the rows and insert them into a String [], then insert the String[]'s into an arraylist, and finally return the arraylist to use it in other class.

public ArrayList<String[]> getWeekUni() {
    SQLiteDatabase db = helper.getWritableDatabase();

    String[] columns = { MenuDataBase.SOPA, MenuDataBase.FONDO1,
            MenuDataBase.REFRESCO, MenuDataBase.CALORIAS, };

    ArrayList<String[]> weekdays = new ArrayList<String[]>();

    String[] rowDay = new String[4];

    Cursor cursor = db.query(MenuDataBase.TABLE_UNI, columns, null, null,
            null, null, null);

    while (cursor.moveToNext()) {
        cursor.moveToNext();
        int index1 = cursor.getColumnIndex(MenuDataBase.SOPA);
        int index2 = cursor.getColumnIndex(MenuDataBase.FONDO1);
        int index3 = cursor.getColumnIndex(MenuDataBase.REFRESCO);
        int index4 = cursor.getColumnIndex(MenuDataBase.CALORIAS);
        rowDay[0] = cursor.getString(index1);
        rowDay[1] = cursor.getString(index2);
        rowDay[2] = cursor.getString(index3);
        rowDay[3] = cursor.getString(index4);

        Log.d("TEST", rowDay[0]+" Get week");
            Log.d("TEST", rowDay[1]+" Get week");

        int i = 0;
        weekdays.add(rowDay);

        Log.d("TEST",Integer.toString(weekdays.size()));
        Log.d("TEST", weekdays.get(i)[0].toString());
        Log.d("TEST", weekdays.get(i)[1].toString());
        i++;
    }

    Log.d("TEST", weekdays.get(0)[0].toString()+" weekdays");
    Log.d("TEST", weekdays.get(0)[1].toString()+" weekdays");
    Log.d("TEST", weekdays.get(3)[0].toString()+" weekdays");
    Log.d("TEST", weekdays.get(3)[1].toString()+" weekdays");

    db.close();
    return weekdays;
}

And my log cat is this:

06-28 10:54:44.160: D/TEST(13577): Semola Get week
06-28 10:54:44.160: D/TEST(13577): Garbanzos a la Americana Get week
06-28 10:54:44.160: D/TEST(13577): 1
06-28 10:54:44.160: D/TEST(13577): Semola
06-28 10:54:44.160: D/TEST(13577): Garbanzos a la Americana
06-28 10:54:44.160: D/TEST(13577): Juliana Get week
06-28 10:54:44.160: D/TEST(13577): Aeropuerto Get week
06-28 10:54:44.160: D/TEST(13577): 2
06-28 10:54:44.160: D/TEST(13577): Juliana
06-28 10:54:44.160: D/TEST(13577): Aeropuerto
06-28 10:54:44.160: D/TEST(13577): Chupe de Carne Get week
06-28 10:54:44.160: D/TEST(13577): Ajiaco con Hamburguesa de Casa Get week
06-28 10:54:44.160: D/TEST(13577): 3
06-28 10:54:44.160: D/TEST(13577): Chupe de Carne
06-28 10:54:44.160: D/TEST(13577): Ajiaco con Hamburguesa de Casa
06-28 10:54:44.160: D/TEST(13577): Moron Get week
06-28 10:54:44.160: D/TEST(13577): Trigo Guizado con Pollito al Horno Get week
06-28 10:54:44.160: D/TEST(13577): 4
06-28 10:54:44.160: D/TEST(13577): Moron
06-28 10:54:44.160: D/TEST(13577): Trigo Guizado con Pollito al Horno
06-28 10:54:44.160: D/TEST(13577): Crema de Verduras Get week
06-28 10:54:44.160: D/TEST(13577): Picante de Carne Get week
06-28 10:54:44.160: D/TEST(13577): 5
06-28 10:54:44.160: D/TEST(13577): Crema de Verduras
06-28 10:54:44.160: D/TEST(13577): Picante de Carne
06-28 10:54:44.160: D/TEST(13577): Crema de Verduras
06-28 10:54:44.160: D/TEST(13577): Picante de Carne weekdays
06-28 10:54:44.160: D/TEST(13577): Crema de Verduras weekdays
06-28 10:54:44.160: D/TEST(13577): Picante de Carne weekdays
06-28 10:54:44.170: D/TEST(13577): Crema de Verduras weekdays

the problem here is that after the while loop ends the weekdays objects set their values as if they would all be the same and you can see that in the last logs I'm showing

I really have no idea why this is happening and I already tried changing the while loop for a for loop

1
  • Just keep in mind to close your cursor too! Commented Jun 28, 2014 at 16:10

2 Answers 2

1

Made a liitle refactoring to make it work. Just have a look at it:

You have to create a new Object on every loop to not change the exsiting ones, which are in your List.

public ArrayList<String[]> getWeekUni() {
    SQLiteDatabase db = helper.getWritableDatabase();

    String[] columns = { MenuDataBase.SOPA, MenuDataBase.FONDO1,
            MenuDataBase.REFRESCO, MenuDataBase.CALORIAS, };

    ArrayList<String[]> weekdays = new ArrayList<String[]>();



    Cursor cursor = db.query(MenuDataBase.TABLE_UNI, columns, null, null,
            null, null, null);

    while (cursor.moveToNext()) {
        // cursor.moveToNext(); //<-- You already did that one line above!!!!
        String[] rowDay = new String[4]; //init here to create a new object and add it not to override existing object references!!!

        int index1 = cursor.getColumnIndex(MenuDataBase.SOPA);
        int index2 = cursor.getColumnIndex(MenuDataBase.FONDO1);
        int index3 = cursor.getColumnIndex(MenuDataBase.REFRESCO);
        int index4 = cursor.getColumnIndex(MenuDataBase.CALORIAS);
        rowDay[0] = cursor.getString(index1);
        rowDay[1] = cursor.getString(index2);
        rowDay[2] = cursor.getString(index3);
        rowDay[3] = cursor.getString(index4);

        Log.d("TEST", rowDay[0]+" Get week");
            Log.d("TEST", rowDay[1]+" Get week");

        //int i = 0;
        weekdays.add(rowDay);

        Log.d("TEST",Integer.toString(weekdays.size()));
        // not i to get last element, get last element with weekdays.size() - 1
        Log.d("TEST", weekdays.get(weekdays.size() - 1)[0].toString());
        Log.d("TEST", weekdays.get(weekdays.size() - 1)[1].toString());
        // i++;
    }

   //To get All entries make it that way
   for(String[] item : weekdays){
        Log.d("TEST", "" + item[0]);
        Log.d("TEST", "" + item[1]);
        Log.d("TEST", "" + item[2]);
        Log.d("TEST", "" + item[3]);
   }

    cursor.close();
    db.close();

    return weekdays;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Well... that worked Can you explain why I should not override? I might know the answer but after some hours thinking about how to fix that might have damaged my brain. Thank you :)
Ok, so you have a reference to a rowDay in your ArrayList of weekDays. In the nex Loop step you are overriding exactly the same reference from rowday, becaus it is still the same. So what happens is, that all references to that sepceific rowDay object are getting updated! The ArrayList does not store a copy of the rowDay, it stores a reference to the object. and you are manipulating that object every loop. Do you get it?
No problem, ran into the same issue years ago and it felt like cracking my brain. Good look!
0

You need to create a new instance of your string array rowDay inside your loop. Currently, you are re-using the array, so the previous values get overwritten (you keep adding the same array to your list, instead of a new array for each row)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.