将值传递给arraylist时,sqlite表的最后一行重复

时间:2018-03-06 10:22:13

标签: android sqlite arraylist

我正在尝试将sqlite表值传递给arraylistsqlite表的值显示正确,但是当它们传递给arraylist时。最后一行重复sqlite表中的行数,这意味着sqlite表是否具有2行我在arraylist得到2个索引,但只有最后插入行的值一次又一次重复。请帮助我

 if (cursor.moveToFirst()) {
        do {
            roleTableModel.setId(cursor.getInt(cursor.getColumnIndex(RoleTableModel.ID)));
            Log.d("Role id", String.valueOf(cursor.getInt(cursor.getColumnIndex(RoleTableModel.ID))));
            roleTableModel.setName(cursor.getString(cursor.getColumnIndex(RoleTableModel.ROLE_NAME)));
            Log.d("Role Name", String.valueOf(cursor.getString(cursor.getColumnIndex(RoleTableModel.ROLE_NAME))));
            roleTableModel.setValue(cursor.getInt(cursor.getColumnIndex(RoleTableModel.ROLE_COUNT)));
            Log.d("Role Value", String.valueOf(cursor.getInt(cursor.getColumnIndex(RoleTableModel.ROLE_COUNT))));
            roleDetails.add(roleTableModel);

3 个答案:

答案 0 :(得分:1)

每次在loop中,您都需要在

中创建RoleTableModel的新实例
        if (cursor.moveToFirst()) {
                do {
                    roleTableModel = new RoleTableModel();//<--add this line
 roleTableModel.setId(cursor.getInt(cursor.getColumnIndex(RoleTableModel.ID)));
                    Log.d("Role id", String.valueOf(cursor.getInt(cursor.getColumnIndex(RoleTableModel.ID))));
                    roleTableModel.setName(cursor.getString(cursor.getColumnIndex(RoleTableModel.ROLE_NAME)));
                    Log.d("Role Name", String.valueOf(cursor.getString(cursor.getColumnIndex(RoleTableModel.ROLE_NAME))));
                    roleTableModel.setValue(cursor.getInt(cursor.getColumnIndex(RoleTableModel.ROLE_COUNT)));
                    Log.d("Role Value", String.valueOf(cursor.getInt(cursor.getColumnIndex(RoleTableModel.ROLE_COUNT))));
                    roleDetails.add(roleTableModel);
        }while(cursor.moveToNext());

答案 1 :(得分:0)

尝试使用以下代码:将while (cursor.moveToNext());放在代码的最后。

  

while(cursor.moveToNext());如果没有可用的下一个值,将停止   表

        if (cursor.moveToFirst()) {
            do {
                RoleModel roleTableModel = new RoleModel(); //Create object every time
                roleTableModel.setId(cursor.getInt(cursor.getColumnIndex(RoleTableModel.ID)));
                Log.d("Role id", String.valueOf(cursor.getInt(cursor.getColumnIndex(RoleTableModel.ID))));
                roleTableModel.setName(cursor.getString(cursor.getColumnIndex(RoleTableModel.ROLE_NAME)));
                Log.d("Role Name", String.valueOf(cursor.getString(cursor.getColumnIndex(RoleTableModel.ROLE_NAME))));
                roleTableModel.setValue(cursor.getInt(cursor.getColumnIndex(RoleTableModel.ROLE_COUNT)));
                Log.d("Role Value", String.valueOf(cursor.getInt(cursor.getColumnIndex(RoleTableModel.ROLE_COUNT))));
                roleDetails.add(roleTableModel);

               } while (cursor.moveToNext());

                    Log.e("Total/Count List Size", "" + cList.size()); //ArrayList Size
         }

答案 2 :(得分:0)

请将此cursor.moveToNext()执行的代码放在新行。

您正在为同一个对象定义值,因此它会添加相同的对象引用。

每次在while循环中创建新对象,如下所述。

if (cursor.moveToFirst()) {
    do {

    RoleModel roleTableModel = new RoleModel();

     roleTableModel.setId(cursor.getInt(cursor.getColumnIndex(RoleTableModel.ID)));
        Log.d("Role id", String.valueOf(cursor.getInt(cursor.getColumnIndex(RoleTableModel.ID))));
        roleTableModel.setName(cursor.getString(cursor.getColumnIndex(RoleTableModel.ROLE_NAME)));
        Log.d("Role Name", String.valueOf(cursor.getString(cursor.getColumnIndex(RoleTableModel.ROLE_NAME))));
        roleTableModel.setValue(cursor.getInt(cursor.getColumnIndex(RoleTableModel.ROLE_COUNT)));
        Log.d("Role Value", String.valueOf(cursor.getInt(cursor.getColumnIndex(RoleTableModel.ROLE_COUNT))));
        roleDetails.add(roleTableModel);

    } while(cursor.moveToNext());
}