我有一个Spinner,它有16个值。 24,24 1/16,24 2/16 .... 25.我要选择一个,然后将其放入double(decimal)形式的变量“ w”中。我想在下面的计算中使用该变量。为了简单起见,我只编写了三个if语句,但是只要知道它会以某种方式影响它,我就会知道其中有16个。但是在计算中,它们显示为未定义。
public void calculate(View view) {
EditText length_find = findViewById(R.id.feet);
EditText pounds_find = findViewById(R.id.pounds);
DecimalFormat df = new DecimalFormat("###.###");
Spinner width_select = findViewById(R.id.width_select);
ArrayAdapter<String> myAdapter = new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.width));
myAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
width_select.setAdapter(myAdapter);
width_select.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Object item = parent.getItemAtPosition(position);
if (position == 0){
double w = 24;
}
if (position == 1){
double w = 24.0625;
}
if (position == 2){
double w = 24.125;
}
}
});
try {
double d = .29;
double h = .002;
//This Calculates if Pounds are given
if (length_find.getText().toString().equals("")){
Toast.makeText(MainActivity.this, "Given Pounds", LENGTH_LONG).show();
double pounds = Double.parseDouble(pounds_find.getText().toString());
//THIS IS THE MATH PORTION
double length_d = (pounds / (w * h * d * 12));
String length = Double.toString(Double.parseDouble(df.format(length_d)));
final TextView zTextView = (TextView) findViewById(R.id.length_show);
zTextView.setText("Feet: " + length);
final TextView mTextView = (TextView) findViewById(R.id.pound_show);
mTextView.setText("Pounds: ");
length_find.getText().clear();
pounds_find.getText().clear();
}
//This Calculates if Length is given
if (pounds_find.getText().toString().equals("")){
Toast.makeText(MainActivity.this, "Given Length", LENGTH_LONG).show();
Double length = Double.parseDouble(length_find.getText().toString());
//THIS IS THE MATH PORTION
double answer_pounds_d = (w * h * length * 12 * d);
String answer_pounds = Double.toString(Double.parseDouble(df.format(answer_pounds_d)));
final TextView mTextView = (TextView) findViewById(R.id.pound_show);
mTextView.setText("Pounds: " + answer_pounds);
final TextView zTextView = (TextView) findViewById(R.id.length_show);
zTextView.setText("Feet: ");
length_find.getText().clear();
pounds_find.getText().clear();
}
if((pounds_find).getText().toString().trim().length() > 0 && (length_find).getText().toString().trim().length() > 0){
Toast.makeText(MainActivity.this, "Whata hell you need me for mate!", LENGTH_LONG).show();
}
}
catch(Exception ex) {
//Toast.makeText(this, "Error", Toast.LENGTH_SHORT).show();
}
}
答案 0 :(得分:1)
由于仅在if
内的onItemClick
语句中创建变量,因此只能在这些if
语句内访问变量。我的建议可能是在if
语句之前创建它,然后在语句中为其赋值。为了使用它,您可能必须将所有数学和内容移至onItemClick
内部。
编辑:未经测试,但我猜是这样的:
public void calculate(View view) {
EditText length_find = findViewById(R.id.feet);
EditText pounds_find = findViewById(R.id.pounds);
DecimalFormat df = new DecimalFormat("###.###");
Spinner width_select = findViewById(R.id.width_select);
ArrayAdapter<String> myAdapter = new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.width));
myAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
width_select.setAdapter(myAdapter);
width_select.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Object item = parent.getItemAtPosition(position);
double w = 0;
if (position == 0){
double w = 24;
}
if (position == 1){
double w = 24.0625;
}
if (position == 2){
double w = 24.125;
}
try {
double d = .29;
double h = .002;
//This Calculates if Pounds are given
if (length_find.getText().toString().equals("")){
Toast.makeText(MainActivity.this, "Given Pounds", LENGTH_LONG).show();
double pounds = Double.parseDouble(pounds_find.getText().toString());
//THIS IS THE MATH PORTION
double length_d = (pounds / (w * h * d * 12));
String length = Double.toString(Double.parseDouble(df.format(length_d)));
final TextView zTextView = (TextView) findViewById(R.id.length_show);
zTextView.setText("Feet: " + length);
final TextView mTextView = (TextView) findViewById(R.id.pound_show);
mTextView.setText("Pounds: ");
length_find.getText().clear();
pounds_find.getText().clear();
}
//This Calculates if Length is given
if (pounds_find.getText().toString().equals("")){
Toast.makeText(MainActivity.this, "Given Length", LENGTH_LONG).show();
Double length = Double.parseDouble(length_find.getText().toString());
//THIS IS THE MATH PORTION
double answer_pounds_d = (w * h * length * 12 * d);
String answer_pounds = Double.toString(Double.parseDouble(df.format(answer_pounds_d)));
final TextView mTextView = (TextView) findViewById(R.id.pound_show);
mTextView.setText("Pounds: " + answer_pounds);
final TextView zTextView = (TextView) findViewById(R.id.length_show);
zTextView.setText("Feet: ");
length_find.getText().clear();
pounds_find.getText().clear();
}
if((pounds_find).getText().toString().trim().length() > 0 && (length_find).getText().toString().trim().length() > 0){
Toast.makeText(MainActivity.this, "Whata hell you need me for mate!", LENGTH_LONG).show();
}
}
catch(Exception ex) {
//Toast.makeText(this, "Error", Toast.LENGTH_SHORT).show();
}
}
}
});
答案 1 :(得分:0)
只需将变量“ w”设置为活动的全局变量。因为变量“ w”是“ if语句”范围的局部变量,并且在其外部未定义。