0

I use the following method in my database class. It is not working and i do not see where the problem is. Maybe someone can help me.

public ArrayList<Profile> getAllProfile() {

        db = this.getWritableDatabase();
        Profile profile = new Profile();
        Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_USER, null);
        ArrayList<Profile> profiles = new ArrayList<>();

        if (cursor!=null){
            for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext() ){

                profile.setId(cursor.getInt(cursor.getColumnIndex(ID)));
                profile.setFirstName(cursor.getString(cursor.getColumnIndex(FIRST_NAME)));
                profile.setLastName(cursor.getString(cursor.getColumnIndex(LAST_NAME)));
                profile.setFatherName(cursor.getString(cursor.getColumnIndex(FATHER)));
                profile.setAge(cursor.getInt(cursor.getColumnIndex(AGE)));
                profile.setEmail(cursor.getString(cursor.getColumnIndex(EMAIL)));
                profile.setGender(cursor.getInt(cursor.getColumnIndex(GENDER)));
                profile.setPhoneNumber(cursor.getString(cursor.getColumnIndex(PHONE_NUMBER)));
                profiles.add(profile);
                App.myLog("is : " +profile.getFirstName());
            }
            cursor.close();
            return profiles;
        }
        cursor.close();
        return null;
    }
kAliert
  • 768
  • 9
  • 21
MoH MaD
  • 1
  • 1

1 Answers1

0

The problem seems to be, that you are creating only one instance of your Profile object before the loop and adding only this instance to your list in your loop.

So change your code as follows:

public ArrayList getAllProfile() 
    {
        db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_USER, null);

        if (cursor != null)
        {
            ArrayList<Profile> profiles = new ArrayList<>();

            for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext() )
            {
                Profile profile = new Profile(); // create a new instance in your loop, instead only creating one instance before the loop
                profile.setId(cursor.getInt(cursor.getColumnIndex(ID)));
                profile.setFirstName(cursor.getString(cursor.getColumnIndex(FIRST_NAME)));
                profile.setLastName(cursor.getString(cursor.getColumnIndex(LAST_NAME)));
                profile.setFatherName(cursor.getString(cursor.getColumnIndex(FATHER)));
                profile.setAge(cursor.getInt(cursor.getColumnIndex(AGE)));
                profile.setEmail(cursor.getString(cursor.getColumnIndex(EMAIL)));
                profile.setGender(cursor.getInt(cursor.getColumnIndex(GENDER)));
                profile.setPhoneNumber(cursor.getString(cursor.getColumnIndex(PHONE_NUMBER)));
                profiles.add(profile);
                App.myLog("is : " +profile.getFirstName());
            }
            cursor.close();
            return profiles;
        }

        if(cursor != null)
        {
            cursor.close();
            return null;
        }
    }
kAliert
  • 768
  • 9
  • 21