【问题标题】:Arraylist of String Arrays issue字符串数组问题的 Arraylist
【发布时间】:2014-06-28 16:06:06
【问题描述】:

我正在尝试从 Sqlite 的数据库中获取数据。 首先将行插入到 String[] 中,然后将 String[] 插入到 arraylist 中,最后返回 arraylist 以在其他类中使用。

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;
}

我的日志猫是这样的:

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

这里的问题是,在 while 循环结束后,工作日对象将它们的值设置为好像它们都是相同的,您可以在我显示的最后一个日志中看到这一点

我真的不知道为什么会这样,我已经尝试将 while 循环更改为 for 循环

【问题讨论】:

  • 记住也要关闭你的光标!

标签: java android sqlite arraylist


【解决方案1】:

进行了少量重构以使其正常工作。看看吧:

您必须在每个循环上创建一个新对象,以免更改列表中的现有对象。

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;
}

【讨论】:

  • 嗯...这工作你能解释为什么我不应该覆盖?我可能知道答案,但几个小时后考虑如何解决可能会损坏我的大脑的问题。谢谢你:)
  • 好的,所以您在weekDays 的ArrayList 中有一个rowDay 的引用。在下一个循环步骤中,您将覆盖来自 rowday 的完全相同的引用,因为它仍然是相同的。那么会发生什么,所有对该特定 rowDay 对象的引用都在更新! ArrayList 不存储 rowDay 的副本,它存储对对象的引用。并且您在每个循环中都在操纵该对象。你明白了吗?
  • 没问题,几年前遇到过同样的问题,感觉就像是在敲我的脑袋。好看!
【解决方案2】:

您需要在循环中创建字符串数组rowDay 的新实例。目前,您正在重新使用该数组,因此之前的值会被覆盖(您不断将相同的数组添加到列表中,而不是为每一行添加一个新数组)

【讨论】:

    猜你喜欢
    • 2010-12-27
    • 1970-01-01
    • 2018-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多