我正在尝试将sqlite
表值传递给arraylist
。 sqlite
表的值显示正确,但是当它们传递给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);
答案 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());
}