-1

I have created a database which stores group details.I want to show that group details in a listview.But when i open my app i only see the the last values in the database.why this is happening???

Getting Value from database

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

When i debugged my

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

why this is happeninng, the desired outcome should be

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

Model Class

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;
    }
}
user3844417
  • 139
  • 1
  • 4
  • 12

3 Answers3

3

you are creating to much groupInfo object. Move its instantiation out the while loop

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

    }
Blackbelt
  • 156,034
  • 29
  • 297
  • 305
2

After only a brief glance through your code you appear to be re-instantiating your ArrayList variable each time the loop runs here;

    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;

This means when you return getGroupInfo, it can only ever be an ArrayList with one element. Instead, you should be instantiating your getGroupInfo variable before the loop starts, like so;

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

I hope this helps.

Rudi Kershaw
  • 12,332
  • 7
  • 52
  • 77
1

There

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

        }

you initializing arrayList everytime. you have to initialize arrayList before and outside while loop.

Pradeep Kumar
  • 777
  • 6
  • 21