需要一些帮助!我有一个微调器,用于确定用户是男性还是女性。我希望计算bmr。如果用户是男性,则与女性相比,bmr公式不同。因此,我试图创建一个textview,它将显示微调器项的字符串内容,因为无法在onItemSelected方法中访问该变量。
这就是为什么我有第二部分,我使用if-else语句基本上将文本与字符串进行比较,并希望将bmr传递给另一个活动,但这并不像我一样工作。 m默认情况下得到错误的值。
spGender = (Spinner) findViewById(R.id.spinnerGender);
final TextView tvGender = (TextView)findViewById(R.id.tvGenderTemp);
spGender.setSelection(0);
spGender.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String item = parent.getItemAtPosition(position).toString();
tvGender.setText(item);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
AlertDialog alertDialog = new AlertDialog.Builder(UserProfile.this).create();
alertDialog.setTitle("Alert");
alertDialog.setMessage("Please select a gender!");
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.show();
spGender.setSelection(0);
}
});
//BMR FORMULA PART II
final double bmr;
if ("Female".equals(tvGender.getText().toString()))
{
bmr = (655 + (13.7*userWeight) + (5.0*userHeight) - (6.8*userAge));
}
else
{
bmr = 66 + (13.7 * userWeight) + (1.8*userHeight) - (4.7*userAge);
}
Button btnUPSave = (Button)findViewById(R.id.btnUserProfileSave);
btnUPSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Saved!",
Toast.LENGTH_LONG).show();
Intent in2 = new Intent(getApplicationContext(), CaloriesPage.class);
in2.putExtra("mBMR",bmr);
startActivity(in2);
}
});
答案 0 :(得分:0)
直接从微调器获取所选项目并进行比较
spGender.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if ("Female".equals(spGender.getSelectedItem().toString())){
bmr = (655 + (13.7*userWeight) + (5.0*userHeight) - (6.8*userAge));
}else{
bmr = 66 + (13.7 * userWeight) + (1.8*userHeight) - (4.7*userAge);
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
答案 1 :(得分:0)
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String item = parent.getItemAtPosition(position).toString();
tvGender.setText(item);
if (item .equals("Female")){
bmr = (655 + (13.7*userWeight) + (5.0*userHeight) - (6.8*userAge));
}else{
bmr = 66 + (13.7 * userWeight) + (1.8*userHeight) - (4.7*userAge);
}
}
答案 2 :(得分:0)
试试这个希望会有效
Spinner spGender;
double bmr;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
List<String> spinnerArray = new ArrayList<>();
spinnerArray.add("male");
spinnerArray.add("female");
spGender = (Spinner) findViewById(R.id.spinnerGender);
ArrayAdapter arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, spinnerArray);
spGender.setAdapter(arrayAdapter);
final TextView tvGender = (TextView) findViewById(R.id.tvGenderTemp);
spGender.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String item = parent.getItemAtPosition(position).toString();
tvGender.setText(item);
//BMR FORMULA PART II
double userWeight = 10;
double userHeight = 20;
double userAge = 35;
if (spGender.getSelectedItem().equals("female"))
{
bmr = (655 + (13.7 * userWeight) + (5.0 * userHeight) - (6.8 * userAge));
} else {
bmr = 66 + (13.7 * userWeight) + (1.8 * userHeight) - (4.7 * userAge);
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
alertDialog.setTitle("Alert");
alertDialog.setMessage("Please select a gender!");
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.show();
}
});
Button btnUPSave = (Button) findViewById(R.id.btnUserProfileSave);
btnUPSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Saved!",
Toast.LENGTH_LONG).show();
Intent in2 = new Intent(getApplicationContext(), Main2Activity.class);
in2.putExtra("mBMR", bmr);
startActivity(in2);
}
});
}