我有一个问题,我无法弄清楚如何解决。我的代码如下:
package com.app.BoomBase;
import android.app.Activity;
import android.app.LauncherActivity.ListItem;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
public class Distance extends Activity implements OnItemSelectedListener {
Button Calc;
Spinner spType, spForce, spWeight;
double FarstCatch, TrickCatch, MTA, LongDistance ;
String[] types = { "Farst Catch", "Trick Catch", "MTA" , "Long Distance" };
EditText etWeight;
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.distance);
super.onCreate(savedInstanceState);
FarstCatch = 100;
TrickCatch = 43.56;
MTA = 32.60 ;
LongDistance = 25;
ArrayAdapter<String> AdapterT = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, types);
spType = (Spinner) findViewById(R.id.spType);
AdapterT.setDropDownViewResource(android.R.layout.simple_spinner_item);
spType.setAdapter(AdapterT);
spType.setPrompt("Select Type");
spType.setOnItemSelectedListener(this);
Button Calc = (Button) findViewById(R.id.button1);
Calc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String selected = spType.getSelectedItem().toString();
Toast.makeText(getApplicationContext(), "Your Boomerang will apoximately fly this far:" + selected, Toast.LENGTH_LONG).show();
}
});
}
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
int position = spType.getSelectedItemPosition();
switch (position){
case 0:
double FarstCatch;
break;
case 1:
double TrickCatch;
break;
case 2:
double MTA;
break;
case 3:
double LongDistance;
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
我想从用户输入(spType)中获取与seleceted case相对应的变量,然后将其用于计算。例子:如果用户选择FarstCatch我想取数字100并在此计算中使用它:R = m / FarstCatch。
我该怎么做?
修改 新代码:
package com.app.BoomBase;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
public class Distance extends Activity implements OnItemSelectedListener {
Button Calc;
Spinner spType, spForce, spWeight;
double FarstCatch, TrickCatch, MTA, LongDistance, r ;
String[] types = { "Farst Catch", "Trick Catch", "MTA" , "Long Distance" };
double[] values = {100, 43.56, 32.6, 25};
EditText etWeight;
int etNum;
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.distance);
super.onCreate(savedInstanceState);
FarstCatch = 100;
TrickCatch = 43.56;
MTA = 32.60 ;
LongDistance = 25;
ArrayAdapter<String> AdapterT = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, types);
spType = (Spinner) findViewById(R.id.spType);
AdapterT.setDropDownViewResource(android.R.layout.simple_spinner_item);
spType.setAdapter(AdapterT);
spType.setPrompt("Select Type");
spType.setOnItemSelectedListener(this);
etWeight = (EditText) findViewById(R.id.etWeight);
Button Calc = (Button) findViewById(R.id.button1);
Calc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String etValue = etWeight.getText().toString();
etNum = Integer.parseInt(etValue);
Toast.makeText(getApplicationContext(), "Your Boomerang will apoximately fly this far:" + r , Toast.LENGTH_LONG).show();
}
});
}
public void onItemSelected1(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// value corresponds to the selected spType
double value = values[arg2];
r = etNum/value;
}
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
int position = spType.getSelectedItemPosition();
switch (position){
case 0:
double FarstCatch;
break;
case 1:
double TrickCatch;
break;
case 2:
double MTA;
break;
case 3:
double LongDistance;
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
但它仍然不起作用..这怎么可能?
答案 0 :(得分:1)
为与数组类型对应的值创建另一个数组。
double[] values = {100, 43.56, 32.6, 25};
接下来,按如下方式实现onItemSelected() -
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// value corresponds to the selected spType
double value = values[arg2];
// Do your calculation
//double R = m/value;
}
注意:arg2是选定的项目位置,因此需要使用getSelectedItem()
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
示例代码:
public class Distance extends Activity implements OnItemSelectedListener {
Button Calc;
Spinner spType, spForce, spWeight;
double selectedValue, r;
String[] types = { "Farst Catch", "Trick Catch", "MTA" , "Long Distance" };
double[] values = {100, 43.56, 32.6, 25};
EditText etWeight;
int etNum;
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.distance);
super.onCreate(savedInstanceState);
ArrayAdapter<String> AdapterT = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, types);
spType = (Spinner) findViewById(R.id.spType);
AdapterT.setDropDownViewResource(android.R.layout.simple_spinner_item);
spType.setAdapter(AdapterT);
spType.setPrompt("Select Type");
spType.setOnItemSelectedListener(this);
etWeight = (EditText) findViewById(R.id.etWeight);
Button Calc = (Button) findViewById(R.id.button1);
Calc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String etValue = etWeight.getText().toString();
etNum = Integer.parseInt(etValue);
r = etNum/selectedValue;
Toast.makeText(getApplicationContext(), "Your Boomerang will apoximately fly this far:" + r , Toast.LENGTH_LONG).show();
}
});
}
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// value corresponds to the selected spType
selectedValue = values[arg2];
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
}