[英]How to make my variable = the item from the Drop Down Menu
我有一個具有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();
}
}
由於您僅在onItemClick
的if
語句中創建變量,因此只能在這些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();
}
}
}
});
只需將變量“ w”設置為活動的全局變量即可。因為變量“ w”屬於'if語句'范圍的局部變量,並且在其外部未定義。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.