0

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.

2
  • Can you output the result of String.format("%d", shift.getEnd()); in your loop alors with the result of shift.getEnd() to see how they compare? Commented Feb 7, 2015 at 1:07
  • Identical. I even hardcoded the values for end with unique numbers, but still all only show the value for the last object. Commented Feb 7, 2015 at 1:09

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

7 Comments

Nope. Still only shows the data from the most recent (last) object.
The values appearing are all correct, but they only belong to the last object in the arrayList
Looking at mShifts in the debugger shows all identical objects. Code for setting the objects is above in original question.
I think that it's coming from there.
You don't need static "things", remove them and give it a try.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.