【问题标题】:how to add android's textview data into arraylist如何将android的textview数据添加到arraylist
【发布时间】:2025-12-19 22:05:16
【问题描述】:

我不是编程专家。我已经在 Android 程序中实现了加速度传感器。 我使用 textview 来显示 x、y 和 z 轴的加速度数据。

现在使用这个传感器数据,我想绘制一个实时图表。那么将文本视图放在数组列表中而不是绘图图是个好主意吗?或者有人有更好的解决方案。

如何将textview的数据添加到arraylist中? 如何绘制这个实时传感器数据的图表?

这是我的程序。

公共类 HelloAndroid 扩展 Activity 实现 SensorEventListener { 私有传感器管理器传感器管理器;

TextView xCoor; // declare X axis object
TextView yCoor; // declare Y axis object
TextView zCoor; // declare Z axis object

@Override
public void onCreate(Bundle savedInstanceState){

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    xCoor=(TextView)findViewById(R.id.xcoor); // create X axis object
    yCoor=(TextView)findViewById(R.id.ycoor); // create Y axis object
    zCoor=(TextView)findViewById(R.id.zcoor); // create Z axis object

    sensorManager=(SensorManager)getSystemService(SENSOR_SERVICE);
    // add listener. The listener will be HelloAndroid (this) class
    sensorManager.registerListener(this,
            sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
            SensorManager.SENSOR_DELAY_NORMAL);


    }

public void onAccuracyChanged(Sensor sensor,int accuracy){

}

public void onSensorChanged(SensorEvent event){


    if(event.sensor.getType()==Sensor.TYPE_ACCELEROMETER){

        // assign directions
        float x=event.values[0];
        float y=event.values[1];
        float z=event.values[2];

        xCoor.setText("X: "+x);
        yCoor.setText("Y: "+y);
        zCoor.setText("Z: "+z);
    }
}

}

【问题讨论】:

    标签: java android


    【解决方案1】:

    您可以观察 TextView 中的更改并运行您的代码,例如。 :

        editText.addTextChangedListener(new TextWatcher() {
                        public void afterTextChanged(Editable s) {
    
                        }
                        public void beforeTextChanged(CharSequence s, int start, int count,
    int after) {
    
                        }
                        public void onTextChanged(CharSequence s, int start, int before,     int count) {
                                String[] array = new String[] {TextView.getText().toString() , TextView1.getText().toString()
    //Correct me if iam Wrong ...  } 
                        }
        });
    

    【讨论】:

      【解决方案2】:

      如果你只是想把TextView放到arrayList中

      List<TextView> textViews= new ArrayList<TextView>();
      textViews.add(TextView1);
      textViews.add(TextView2);
      textViews.add(TextView3);
      

      【讨论】: