I'm trying to add to a LinkedList values from the accelerometer.
private LinkedList<Float> rawValues = new LinkedList<Float>();
public void onSensorChanged(SensorEvent event)
{
int eventType = event.sensor.getType();
switch(eventType)
{
case Sensor.TYPE_ACCELEROMETER:
float accelerometer_z = event.values[2];
rawValues.add(accelerometer_z);
/**
* This printout gives me positions and event values
*/
System.out.println("getAccelerometer size: " +
rawValues.size() + " entry: " + rawValues.getLast());
break;
}
}
So far so good, I get readings and number positions as expected. However, when I'm trying to do stuff with the list in other methods, it appears to be empty!
public String[] getVelocity()
{
String[] velocityString = {"42","42"};
String values = "";
String theInteger = "";
/**
* Returns a 0 size...
*/
int theSize = rawValues.size();
System.out.println("getVelocity size: " + theSize);
LinkedList<Float> slopeValues = calculateSlopeValues( rawValues);
for (int i = 0; i < theSize; i++)
{
values += "Raw:"+i+ " " + String.valueOf(rawValues.get(i)) + "\n";
values += "\tSlope:"+i+ " " + String.valueOf(slopeValues.get(i)) + "\n";
}
theInteger = String.valueOf(Math.round(Collections.max(slopeValues)*100));
/**
* After adding everything to local variables, the list is cleared.
*/
slopeValues.clear();
velocityString[0] = theInteger;
velocityString[1] = values;
return velocityString;
}
getVelocity method is called from the onTouch in the main activity. Shed some light, please?
Regards /M
Overridden onTouch method:
@Override
public boolean onTouch(View view, MotionEvent event)
{
MySensorEventListener eventListener = new MySensorEventListener();
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN:
String[] strings = eventListener.getVelocity();
String velocity_int = strings[0];
String velocity_string = strings[1];
velocityView.setText(velocity_int);
listView.setText(velocity_string);
break;
}
return false;
}
Well, each time onTouch()
is called, you create a new instance of MySensorEventListener
, and ask this new instance for its velocity. Since each MySensorEventListener
instance has its own linked list of raw values, you get an empty list each time.
The listener should probably be created only once, and stored in an instance field of the enclosing class.