ArrayList <model>显示错误的值</model>

时间:2014-08-01 10:40:59

标签: android arraylist android-sqlite

我创建了一个存储组详细信息的数据库。我想在listview中显示该组详细信息。但是当我打开我的应用程序时,我只看到数据库中的最后一个值。为什么会发生这种情况?

从数据库中获取价值

public ArrayList<GroupModel> getAllGroups() {
        SQLiteDatabase database = getWritableDatabase();
        String query = "Select * From " + tableName;
        GroupModel groupModel;
        Cursor c = database.rawQuery(query, null);
        while (c.moveToNext()) {
            getGroupInfo = new ArrayList<GroupModel>();
            groupModel = new GroupModel(c.getString(c.getColumnIndexOrThrow(groupName)), c.getString(c.getColumnIndexOrThrow(createdOn)));
            getGroupInfo.add(groupModel);

        }
        return getGroupInfo;
    }

当我调试我的

1st iteration List value[0]="A","B"
2nd iteration List value[0]="p","q";

为什么会发生这种情况,期望的结果应该是

1st iteration List value[0]="A","B"
2nd iteration List value[1]="p","q";

模型类

public class GroupModel {

    private String groupId, groupName, groupCreatedDate, contactId, contactName, contactNumber;

    public GroupModel() {
    }

    public GroupModel(String groupName, String groupCreatedDate) {

        this.groupName = groupName;
        this.groupCreatedDate = groupCreatedDate;
    }

    public GroupModel(String groupId, String contactId, String contactName, String contactNumber) {
        this.groupId = groupId;
        this.contactId = contactId;
        this.contactName = contactName;
        this.contactNumber = contactNumber;
    }

    public String getGroupId() {
        return groupId;
    }

    public void setGroupId(String groupId) {
        this.groupId = groupId;
    }

    public String getGroupName() {
        return groupName;
    }

    public void setGroupName(String groupName) {
        this.groupName = groupName;
    }

    public String getGroupCreatedDate() {
        return groupCreatedDate;
    }

    public void setGroupCreatedDate(String groupCreatedDate) {
        this.groupCreatedDate = groupCreatedDate;
    }

    public String getContactId() {
        return contactId;
    }

    public void setContactId(String contactId) {
        this.contactId = contactId;
    }

    public String getContactName() {
        return contactName;
    }

    public void setContactName(String contactName) {
        this.contactName = contactName;
    }

    public String getContactNumber() {
        return contactNumber;
    }

    public void setContactNumber(String contactNumber) {
        this.contactNumber = contactNumber;
    }
}

3 个答案:

答案 0 :(得分:3)

您正在创建多个groupInfo对象。将其实例化移出while循环

getGroupInfo = new ArrayList<GroupModel>();
while (c.moveToNext()) {   
        groupModel = new GroupModel(c.getString(c.getColumnIndexOrThrow(groupName)), c.getString(c.getColumnIndexOrThrow(createdOn)));
        getGroupInfo.add(groupModel);

    }

答案 1 :(得分:2)

只需简单浏览一下代码,每次循环运行时,您似乎都会重新实例化ArrayList变量;

    while (c.moveToNext()) {
        getGroupInfo = new ArrayList<GroupModel>(); // <-- HERE!
        groupModel = new GroupModel(c.getString(c.getColumnIndexOrThrow(groupName)), c.getString(c.getColumnIndexOrThrow(createdOn)));
        getGroupInfo.add(groupModel);
    }
    return getGroupInfo;

这意味着当您return getGroupInfo时,它只能是具有一个元素的ArrayList。相反,您应该在循环开始之前实例化getGroupInfo变量,就像这样;

    getGroupInfo = new ArrayList<GroupModel>(); // Moved to here.
    while (c.moveToNext()) {       
        groupModel = new GroupModel(c.getString(c.getColumnIndexOrThrow(groupName)), c.getString(c.getColumnIndexOrThrow(createdOn)));
        getGroupInfo.add(groupModel);
    }

我希望这会有所帮助。

答案 2 :(得分:1)

while (c.moveToNext()) {
            getGroupInfo = new ArrayList<GroupModel>();
            groupModel = new GroupModel(c.getString(c.getColumnIndexOrThrow(groupName)), c.getString(c.getColumnIndexOrThrow(createdOn)));
            getGroupInfo.add(groupModel);

        }

每次都初始化arrayList。 你必须在while循环之前和之外初始化arrayList。