Search code examples
javaandroidarraysarraylistaccelerometer

Pushing accelerometer data into an array


I'm having trouble recording the Z-axis data from the accelerometer in an array.

I think I'm probably failing on some basic java rules, but here's what I'm trying to do:

    private ArrayList<Float[]> z = new ArrayList<Float[]>();
            protected void onCreate(Bundle savedInstanceState) {

                    SensorManager manager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        Sensor accelerometer = manager
                .getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
}
            public void onSensorChanged(SensorEvent event) {


                float x = event.values[0];
                float y = event.values[1];
                z.add(event.values[2]);
}

But whenever I try to add to the arraylist I get:

"The method add(Float[]) in the type ArrayList is not applicable for the arguments (float)"

How can I add the z axis data to an array?


Solution

  • Its because your ArrayList of Float[] type.

    Replace the following,

     private ArrayList<Float[]> z = new ArrayList<Float[]>();
    

    with

     ArrayList<Float> z = new ArrayList<Float>();