将两个值放在一个Arraylist中,然后在一个for循环中读取它们

时间:2017-04-16 20:37:21

标签: java android arrays arraylist

是否可以在for循环期间从我的arraylist列表中读取 valueAfterMovingAverage 的值和 FrontalSpeed 的值?我猜想技术问题在我的for循环中。它只读取valueAfterMovingAverage的大小,但不读取存储在列表数组中的FrontalSpeed的值。

因此for循环只读取第一个数组的值。 我应该强调两个值的大小(我的意思是两个变量数组)不​​一样。如何在for循环中写入(进入我的文本文件)值。我的文本文件中的当前结果具有重复的第二个值,这是错误的。必须根据传感器加速度计的读数进行更改。

float valueAfterMovingAverage;
double TotalAccelerate;
ArrayList<Float> list = new ArrayList<Float>();
@Override
public final void onSensorChanged(SensorEvent event) {
// The light sensor returns a single value.
// Many sensors return 3 values, one for each axis.
if (isListening) {
    double xx = event.values[0];
    double yy = event.values[1];
    double zz = event.values[2];
    TotalAccelerate = Math.round(Math.sqrt(Math.pow(xx, 2)
            + Math.pow(yy, 2)
            + Math.pow(zz, 2)));
    Log.i(DEBUG, "Accelerometer = " + TotalAccelerate);
    FrontalSpeed = Math.round(Math.sqrt(Math.pow(xx, 2)
            + Math.pow(zz, 2)));

    Log.i(DEBUG, "list values " + listPeaks);
    MovingAverage ma = new MovingAverage(2);
    ma.newNum(TotalAccelerate);
    valueAfterMovingAverage = (float) ma.getAvg();
    sensorText.setText(String.valueOf(valueAfterMovingAverage));
    Log.i(DEBUG, "Moving avg:  " + valueAfterMovingAverage);
      list.add(valueAfterMovingAverage);
      list.add(FrontalSpeed);
      Log.i(DEBUG,"Size of List: "+ list.size());

  }

}

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
   ...
   ...


OnStore = (ToggleButton) findViewById(R.id.onStore);
OnStore.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View view) {
        if (OnStore.isChecked()) {
            //
            // set listening flag to true
            //
            isListening = true;
            Toast.makeText(getApplicationContext(), "Start...", Toast.LENGTH_LONG).show();

        } else if (!OnStore.isChecked()) { //
            // set listening flag to false
            //
            isListening = false;

            try {
                for (double valueFromList : list) {
                    String space = "\n";
                    byte[] convert = space.getBytes();
                    fileOutputStream.write(convert);
                    String finalData;

                    finalData = String.valueOf(valueFromList);

                    String line = finalData + " " + FrontalSpeed + "\n";
                    Log.i(DEBUG, "speed: " + FrontalSpeed);
                    fileOutputStream.write(line.getBytes());

                    Log.i(DEBUG, "ans: " + finalData);

                }
                fileOutputStream.flush();
                fileOutputStream.close();
                Toast.makeText(getApplicationContext(), "Message saving", Toast.LENGTH_LONG).show();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            Toast.makeText(getApplicationContext(), "Message Stopped.", Toast.LENGTH_LONG).show();
        }


    }
});

1 个答案:

答案 0 :(得分:0)

我会选择两个单独的Arrayslist's。一个用于保存valueAfterMovingAverage的数量,另一个用于FrontalSpeed。只是一个建议。