我有一个Java代码文件,它是两个文件的混合体。原始现有的代码+用于绘制随机数据图形的代码。(感谢Dan和keshav帮助使其工作)。
该图形在应用程序上显示,以绘制随机数据。我想将随机数据更改为我的数据。我在应用程序更前面有外部加速度计值,这些值是双精度,并希望使用这些值进行绘图。
下面是写入文件时出现双精度的代码:
@Override
public void onDataRecieved(TiSensor<?> sensor, String text) {
if (sensor instanceof TiAccelerometerSensor) {
final TiAccelerometerSensor accSensor = (TiAccelerometerSensor) sensor;
float[] values = accSensor.getData();
renderer.setRotation(values);
viewText.setText(text);
/**
* The code here, when button clicked, adds the contents of the
* edittext to the same file as x, y, and z
*/
Button confirmButton = (Button) findViewById(R.id.baddNametoFile);
confirmButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
try {
EditText bodyText;
bodyText = (EditText) findViewById(R.id.ETName);
// bodyText.setInputType(InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS);
// CAPS LOCK
String name = bodyText.getText().toString();
PrintWriter out = new PrintWriter(new BufferedWriter(
new FileWriter(
"/sdcard/YS Data/Accelerometer.html",
true)));
out.println("<hr><br><br><br><h2 style=padding-left:20px;>"
+ name + "'s Data below" + "</h2><br>");
out.flush();
// new arraylist
out.close();
} catch (IOException e) {
Log.e(TAG, "Could not write file " + e.getMessage());
e.printStackTrace();
}
}
});
/**
* Code Below is for splitting the string (text) into three strings
* (x,y,z) of just numbers and then printing them to a file
*/
double doubx;
double douby;
double doubz;
String accval = text;
try {
PrintWriter writer = new PrintWriter(new BufferedWriter(
new FileWriter("sdcard/YS Data/Accelerometer.html",
true)));
String[] tempArr = accval.split("\\s+");
String x1 = tempArr[0];
String y1 = tempArr[1];
String z1 = tempArr[2];
String[] valxsplit = x1.split("=");
String x = valxsplit[1];
String[] valysplit = y1.split("=");
String y = valysplit[1];
String[] valzsplit = z1.split("=");
String z = valzsplit[1];
doubx = Double.parseDouble(x);
douby = Double.parseDouble(y);
doubz = Double.parseDouble(z);
writer.println("<h3 style=padding-left:20px;>" + "x is "
+ doubx + "<br>" + "y is " + douby + "<br>"
+ "Force is " + doubz + "</h3><br>");
writer.close();
} catch (IOException e) {
/** notification possibly */
e.printStackTrace();
}
}
}
//Then lots of code to do other things... then this in graph code
private int generateRandomNum() {
Random randomGenerator = new Random();
int randomInt = randomGenerator.nextInt(10);
return randomInt;
}
protected class Update extends AsyncTask<Context, Integer, String> {
@Override
protected String doInBackground(Context... params) {
int i = 0;
while (true) {
try {
Thread.sleep(700);
x11 = x11 + 5;
y11 = generateRandomNum();
publishProgress(i);
i++;
} catch (Exception e) {
}
}
// return "COMPLETE!";
}
// -- gets called just before thread begins
@Override
protected void onPreExecute() {
super.onPreExecute();
}// then more code... unrelated
我想将上面的代码中的
y11
更改为顶部的doubx
。当我尝试在上面的代码中只更改y11
为doubx
时,我会收到一个错误,显示doubx无法解析为变量
。我想知道是否可以使用意图(intents)沿着代码传递double值,以及它们所具有的值,到我希望再次使用它们的地方。然而,我知道意图通常用于在活动之间传递数据,而不是在活动内部传递数据。不过,反过来说我对Java很陌生,并不知道是否有比我想象中更简单的方法来实现这一点。如果有人知道,请帮忙指教。
提前致谢。
注:我已经添加了在顶部定义它们的代码。
double doubx etc...
它成功了!需要略微更改代码,我也已经编辑了。感谢Ivan和sush的答案:D