Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6646741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:27:13+00:00 2026-05-26T00:27:13+00:00

I am writing an app that needs to keep the values that come from

  • 0

I am writing an app that needs to keep the values that come from the accelerometer. I am doing this by using an ArrayList and then when onAccelerationChanged(…) gets called I simply add those values to the list. The problem is that after so many values (never consistent) get pushed into the list it starts overwriting the values with the value I am adding. I figured I was doing something stupid so I took the ‘Accelerometer Play Sample’ and modified onSensorChanged(…) to do the same. Doing this replicates the issue.

variable added to class:

private ArrayList<SensorEvent> _accelData = new ArrayList<SensorEvent>();

onSensorChanged:

@Override
public void onSensorChanged(SensorEvent event)
{
  if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER)
    return;

  Log.e("Event to Add", event.timestamp + ", " + event.accuracy + ", " + event.values[0] + ", " + event.values[1]
      + ", " + event.values[2]);

  _accelData.add(event);

  if (_accelData.size() > 25) {
    for (SensorEvent tmp_event : _accelData) {
      Log.e("Events in list", tmp_event.timestamp + ", " + tmp_event.accuracy + ", " + tmp_event.values[0] + ", "
          + tmp_event.values[1] + ", " + tmp_event.values[2]);
    }

    _accelData.clear();
  }
}

On the left is each event that was supposed to be added and on the right is the list printed out after 26 adds were performed. As you can tell the values that were supposed to be added don’t match the list itself and there is a ton of repetition of the last and second to last value in the list.

enter image description here

I fixed this by making a new class that takes in an event and copies those values. Then I make the ArrayList take that class instead. It works but I don’t understand why and was hoping someone could educate me since I am new to Java and Android.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T00:27:14+00:00Added an answer on May 26, 2026 at 12:27 am

    This is documented:

    NOTE: The application doesn’t own the event object passed as a parameter and therefore cannot hold onto it. The object may be part of an internal pool and may be reused by the framework.

    By adding a reference to a list you’re doing exactly what you’re told not to.

    Basically the framework is repeatedly passing you a reference to the same object, changing the values within the object each time.

    If you need to hold on to the different values, you’ll need to copy them into your own object and add that reference to your list (or clone the value you’re passed into another SensorEvent if you want).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing an app that needs to get the longitude and latitude from the
I'm writing an app for android that needs to parse data from an XML
I'm fairly new to Java and am writing an app that needs an XML
I'm writing an winforms app that needs to set internet explorer's proxy settings and
I'm writing a Windows .NET app that needs a WYSIWYG text editor control that
I'm writing a simple web app in PHP that needs to have write access
I am writing an application that needs to bring window of an external app
I'm writing an app that supposed to copy a bunch of files from one
I'm writing an app that needs to output Decimals of varying lengths, and varying
I'm writing an app that needs to function differently if it is running on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.