如何在Android中选择Spinner项目时更新TextView?

时间:2014-02-17 11:22:54

标签: java android textview spinner

在一项活动中,我有几个Spinners和TexView。我希望每当用户通过调用类中定义的方法更改微调器中的值时,TextView的内容都会更改。

这就是我的所作所为:

解开课程中的项目:

private Spinner spinner_age, spinner_weight, spinner_sex, spinner_height,
        spinner_level_activity, spinner_goal;
private  TextView textView_to_goal_disp, textView_neededCal_disp;

我@Override on on onCreate方法并通过id找到vi:

    spinner_age = (Spinner) findViewById(R.id.spinner_age);
    spinner_weight = (Spinner) findViewById(R.id.spinner_weight);
    spinner_sex = (Spinner) findViewById(R.id.spinner_sex);
    spinner_height = (Spinner) findViewById(R.id.spinner_height);
    spinner_goal = (Spinner) findViewById(R.id.spinner_goal);
    spinner_level_activity = (Spinner) findViewById(R.id.spinner_level_activity);

    textView_to_goal_disp = (TextView) findViewById(R.id.textView_to_goal_display);
    textView_neededCal_disp = (TextView) findViewById(R.id.textView_needed_calories_display);

再次在onCreate方法中,我定义了OnItemSelectedListener并将其分配给Spinners:

    OnItemSelectedListener itemSelect = new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

            // My method i want to execute if an item is selected or changed
            calculateParameters(spinner_age, spinner_weight, spinner_sex, spinner_height, spinner_goal,
                    spinner_level_activity, textView_to_goal_disp, textView_neededCal_disp);

        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {

        }           
    };

    spinner_age.setOnItemSelectedListener(itemSelect);
    spinner_goal.setOnItemSelectedListener(itemSelect);
    spinner_height.setOnItemSelectedListener(itemSelect);
    spinner_level_activity.setOnItemSelectedListener(itemSelect);
    spinner_sex.setOnItemSelectedListener(itemSelect);
    spinner_weight.setOnItemSelectedListener(itemSelect);

这是我在类中定义的calculateParameters方法:

public static void calculateParameters(Spinner sp_age, Spinner sp_weight, Spinner sp_sex,
        Spinner sp_height, Spinner sp_goal, Spinner sp_lvl_act, TextView txt_to_goal_disp,
        TextView need_cal_disp) {

    int weight, height, age;

    weight = Integer.parseInt(sp_weight.getSelectedItem().toString());
    height = Integer.parseInt(sp_height.getSelectedItem().toString());
    age = Integer.parseInt(sp_age.getSelectedItem().toString());

    float[] BMR = new float[2];
    float[] TDEE = new float[2];

    BMR[0] = (float) (66 + (13.7 * weight) + (5 * height) - (6.8 * age)); // Men
    BMR[1] = (float) (655.1 + (9.563 * weight) + (1.850 * height) - (4.676 * age)); // Woman



    // Initializing 0 - Men ; 1 - Women

    // Activity factor
    float[] AF = new float[2];

    switch (sp_lvl_act.getSelectedItemPosition()) {
    case 0:
        for(int i=0; i<2;i++){
            AF[i] =     (float) (BMR[i] * 1.2);     // (Little or no exercise, desk job);
        }
        break;
    case 1:for(int i=0; i<2;i++){
        AF[i] =(float) (BMR[i] * 1.375);// (Light exercise/sports 1-3 days a week);
    }
        break;
    case 2:
        for(int i=0; i<2;i++){
        AF[i] =(float) (BMR[i] * 1.55); // (Moderate exercise/sports 3-5 days a week);
        }
        break;
    case 3:
        for(int i=0; i<2;i++){
        AF[i] =(float) (BMR[i] * 1.725); // (Hard exercise/sports 6-7 days a week);
        }
        break;
    case 4:
        for(int i=0; i<2;i++){
        AF[i] =(float) (BMR[i] * 1.9);// (Hard daily exercise/sports and physical job or 2x day training, ie;
        }
        break;

    default:
        break;
    }

        // Maintance level
    for(int i =0; i < 2; i++){
        TDEE[i] = BMR[i] * AF[i];
    }

    switch (sp_sex.getSelectedItemPosition()) {
    case 0:
        txt_to_goal_disp.setText(Float.toString(TDEE[0]));
        break;
    case 1:
        need_cal_disp.setText(Float.toString(TDEE[1]));
        break;

    default:
        break;
    }

    int currentWeight = Integer.parseInt(sp_weight.getSelectedItem().toString());
    int goalWeight = Integer.parseInt(sp_goal.getSelectedItem().toString());
    txt_to_goal_disp.setText(Math.abs(currentWeight)-goalWeight);

} 

当我开始该活动时应用程序崩溃。

这是我的LogCat和我得到的错误:

FATAL EXCEPTION: main
Process: com.example.appscan5, PID: 13339
android.content.res.Resources$NotFoundException: String resource ID #0x0
at android.content.res.Resources.getText(Resources.java:244)
at android.widget.TextView.setText(TextView.java:3888)
at com.example.appscan5.SettingParameters.calculateParameters(SettingParameters.java:164)
at com.example.appscan5.SettingParameters$1.onItemSelected(SettingParameters.java:45)
at android.widget.AdapterView.fireOnSelected(AdapterView.java:893)
at android.widget.AdapterView.access$200(AdapterView.java:48)
at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:861)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

Textview只接受字符串而不是int或float ... 写下面的

txt_to_goal_disp.setText(""+(Math.abs(currentWeight)-goalWeight));