0

I got an ArrayList<MyClass> that is filled with Measure objects, i got from the acceleration sensor of a cell phone. The objects have another ArrayList with 150 measure values. When I investigate on this Arraylist while debugging, it is getting filled correctly. But after i want to take that ArrayList with a button event, the values have changed. the number of objects inside stays the same, but its alway one identical object, that wasnt in the ArrayList before. I hope, you could give me a little hint what to do here. Its so confusing...

public class StartDesk extends AppCompatActivity implements SensorEventListener{

    private ArrayList<SingleSwing> alSwingsCurSession;
    private ArrayList<SingleAccValue> alSavedAccValues;
    private AudioSensorClass asc;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start_desk);
        ...

        alSavedAccValues = new ArrayList<>();
        bRecording = false;

        btnStart = (Button) findViewById(R.id.btnStart);
        btnStart.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                RecordButtonHandler();
            }
        });

    }

    private void RecordButtonHandler() {
        if(!bRecording) {
            alSwingsCurSession = new ArrayList<>();
            ...

        }
        else {
            btnStart.setText("Start Recording");
            if (alSwingsCurSession.size() > 0) {
               ...
            }
            bRecording = false;
        }
    }


   ...


    private void SaveSwingINI(ArrayList<SingleAccValue> al_SavedAccValues) {
        if(al_SavedAccValues.size()>=150) {
            SingleSwing swing = new SingleSwing();
            swing.setAl_MeasureData(al_SavedAccValues);
            alSwingsCurSession.add(swing);
        }
    }
...
}

alSwingsCurSession has other values, when calling it at the button events, then it gets in the SaveSwingINI method. I got no static fields in my Classes.

iGzorn
  • 11
  • 6
  • Possible duplicate of [Why does my ArrayList contain N copies of the last item added to the list?](http://stackoverflow.com/questions/19843506/why-does-my-arraylist-contain-n-copies-of-the-last-item-added-to-the-list) – shmosel Feb 12 '17 at 09:47
  • I just double checked there are no static fields in my classes – iGzorn Feb 12 '17 at 10:10
  • Cannot really tell from the information given here. It will help a lot if you [create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). – Ole V.V. Feb 12 '17 at 11:09

1 Answers1

1

"al_SavedAccValues" is always the same ArrayList<> from one point of the memory. making a new "al_SavedAccValues" Arraylist in the "SaveSwingINI" method was helpful.

private void SaveSwingINI(ArrayList<SingleAccValue> al_SavedAccValues) {
        if(al_SavedAccValues.size()>=150) {
            SingleSwing swing = new SingleSwing();
            ArrayList<SingleAccValue> al_SwingValues = new ArrayList<>(al_SavedAccValues);
            swing.setAl_MeasureData(al_SwingValues);
            alSwingsCurSession.add(swing);
        }
    }
iGzorn
  • 11
  • 6