Search code examples
javaandroidsqlitearraylistandroid-arrayadapter

Android, ListView showing identical data from ArrayList?


The following code:

    int i = 0;
    for(Shift shift : mShifts){
        shifts[i] = shift + "";
        i++;
    }

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getListView().getContext(), android.R.layout.simple_list_item_1, shifts);
    setListAdapter(adapter);

produces a list of all unique Shift objects - something that looks like...

com.name.appname.Shift@529a4ecc
com.name.appname.Shift@529a5010
com.name.appname.Shift@529a5088
com.name.appname.Shift@529a5100
... ect ...

for as many Shift objects I have created. Where mShifts is an ArrayList of Shift objects.

But, when I use the following:

    int i = 0;
    for(Shift shift : mShifts){
        shifts[i] = String.format("%d", shift.getEnd());
        i++;
    }

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getListView().getContext(), android.R.layout.simple_list_item_1, shifts);
    setListAdapter(adapter);

it produces a list of all identical values, when they are verified to be unique. The only change is in the string assignment within the loop.

The identical values are all the time stamp from the most recent Shift object.

shift.getEnd() produces a long value, which is a time stamp (milliseconds) from when an event occurred. Each value is stored in a database, and I have verified (using SQLite Database Browser) that all entries are truly unique.

EDIT 1

I assign mShifts from the DB in the following way:

    ArrayList<Shift> mShifts = new ArrayList<Shift>();

    if(cursor.moveToFirst()){
        int i = 0;
        do{
            Shift shift = new Shift(getStringFromColumnName(cursor, ShiftsSQLiteHelper.COLUMN_SHIFT_NAME),
                    getStringFromColumnName(cursor, ShiftsSQLiteHelper.COLUMN_SHIFT_EMPLOYER),
                    getLongFromColumnName(cursor, ShiftsSQLiteHelper.COLUMN_SHIFT_START),
                    i);

            mShifts.add(shift);
            i++;
        }while(cursor.moveToNext());
    }

It is important to note that I have hard-coded the value for end with the variable i above - that way I know for certain that it is not an error reading from the DB. There are 12 objects, for whatever reason the printout in the array list shows all having end=11 (the value of the last object).

The issue is not with assigning the ArrayList, but rather displaying it.

Edit 2

Viewing the mShifts arrayList in the debugger (while assigning the list) shows that on each loop ALL the values in the array list are set to the most recent one.

So the offending code is the assignment loop.

Shift class

public class Shift {
    private static String mName;
    private static String mEm;
    private static long mStart;
    private static long mEnd;

    public Shift(String name, String em, long start, long end) {
        setName(name);
        setEm(em);
        setStart(start);
        setEnd(end);
    }

    public static String getName() {
        return mName;
    }
    public static void setName(String name) {
        mName = name;
    }

    public static String getEm() {
        return mEm;
    }
    public static void setEm(String em) {
        mEm = em;
    }

    public static long getStart() {
        return mStart;
    }
    public static void setStart(long start) {
        mStart = start;
    }

    public static long getEnd() {
        return mEnd;
    }
    public static void setEnd(long end) {
        mEnd = end;
    }
}

Please let me know if you need any other code snippets.


Solution

  • A static variable is a class attribute not an object attribute, any code referring to the variable is referring to the exact same data.

    In your case your overwriting the previous content of Shift attributes.