我将这个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
更改为doub x
时,我收到错误doubx cannot be resolved to a variable
。
我想知道是否可以使用Intent将代码中的double传递给他们所拥有的值到我想要再次使用它的位置,但是我知道意图通常用于在<之间传递数据 / em>活动,而不是中的。然后我又是Java的新手,不知道是否有一种比我想的更简单的方法。如果有人知道请求帮助的方法。
提前致谢
编辑:我添加了代码以在顶部定义它们:
double doubx etc...
它有效!好吧,代码中有一些变化,我也编辑过。感谢伊万并为他们的答案而努力:D
答案 0 :(得分:2)
在trycatch之外定义所有变量我的意思是将它们作为成员变量。增加范围。所有都是localscope中的变量。
double doubx = Double.parseDouble(x);
double douby = Double.parseDouble(y);
double doubz = Double.parseDouble(z);
答案 1 :(得分:1)
将douby,douby,douby设为外类的成员变量
像这样:
class OuterClass {
double doubx;
double douby;
double doubz;
void method {
String accval = text;
try {
PrintWriter writer = new PrintWriter(new BufferedWriter(
new FileWriter("sdcard/YS Data/Accelerometer.html",
true)));
.....
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();
}
}
protected class Update extends AsyncTask<Context, Integer, String> {
.....
}
}