如何将整数值包含到微调器数组中

时间:2015-02-08 19:48:31

标签: java android

所以我有2个微调器,第1个允许用户选择食物类型,然后使用该类型的选项填充第2个微调器。 (早餐=水果,午餐=三明治等)。

我希望在这些之后添加整数值(例如苹果62千卡/ 100克)以计算:选择的食物卡路里(每100克中的int)乘以输入到editText(id food_Grams)的重量。

所以我希望第二个微调器中的每个食物项都有一个整数值,该值将用于计算运行的卡路里总量。我将如何在提供的代码中执行此操作?

 public class ActivityAdd extends MainActivity {

public Spinner spinner1, spinner2;
public Button btnSubmit;
boolean app_started = true;


protected void onCreate(Bundle x) {

    super.onCreate(x);
    setContentView(R.layout.activityadd);

    chooseCategory();
    chooseBreakfast();
    chooseLunch();
    chooseDinner();
    chooseSnack();
    addListenerOnButton();
    addListenerOnSpinnerItemSelection();

}

public void chooseCategory() {

    spinner1 = (Spinner) findViewById(R.id.foodtype);
    ArrayAdapter<CharSequence> catAdapter = ArrayAdapter.createFromResource(this, R.array.foodtype, android.R.layout.simple_spinner_item);
    catAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner1.setAdapter(catAdapter);
    spinner1.setOnItemSelectedListener(new planOnSelectedListener());
}

private void chooseBreakfast() {

    spinner2 = (Spinner) findViewById(R.id.planspinner);
    List<String> planlist = new ArrayList<String>();
    planlist.add("Cereal");
    planlist.add("Porrige");
    planlist.add("Fruit");
    ArrayAdapter<String> planAdapter1 = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, planlist);
    planAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner2.setAdapter(planAdapter1);
}

private void chooseLunch() {

    spinner2 = (Spinner) findViewById(R.id.planspinner);
    List<String> planlist1 = new ArrayList<String>();
    planlist1.add("Sandwich");
    planlist1.add("Omelette");
    ArrayAdapter<String> planAdapter2 = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, planlist1);
    planAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner2.setAdapter(planAdapter2);
}

private void chooseDinner() {

    spinner2 = (Spinner) findViewById(R.id.planspinner);
    List<String> planlist2 = new ArrayList<String>();
    planlist2.add("Pizza");
    planlist2.add("Pasta");
    planlist2.add("Pie");
    planlist2.add("Fish");
    ArrayAdapter<String> planAdapter3 = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, planlist2);
    planAdapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner2.setAdapter(planAdapter3);
}

private void chooseSnack() {

    spinner2 = (Spinner) findViewById(R.id.planspinner);
    List<String> planlist2 = new ArrayList<String>();
    planlist2.add("Crisps");
    planlist2.add("Biscuit");
    ArrayAdapter<String> planAdapter3 = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, planlist2);
    planAdapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner2.setAdapter(planAdapter3);
}

public void addListenerOnSpinnerItemSelection() {

    spinner2 = (Spinner) findViewById(R.id.planspinner);
    spinner2.setOnItemSelectedListener(new CustomOnItemSelectedListener());
}

// get the selected dropdown list value
public void addListenerOnButton() {

    spinner1 = (Spinner) findViewById(R.id.foodtype);
    spinner2 = (Spinner) findViewById(R.id.planspinner);
    btnSubmit = (Button) findViewById(R.id.button_save);

    btnSubmit.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            Toast.makeText(ActivityAdd.this,
                    "OnClickListener : " +
                            "\nSpinner 1 : "+ String.valueOf(spinner1.getSelectedItem()) +
                            "\nSpinner 2 : "+ String.valueOf(spinner2.getSelectedItem()),
                    Toast.LENGTH_SHORT).show();
        }

    });
}

public class planOnSelectedListener implements OnItemSelectedListener {

    @Override
    public void onItemSelected(AdapterView<?> parent, View v, int pos, long id) {
        if (!app_started) {
            parent.getItemAtPosition(pos);
            if (pos == 0) {
                chooseBreakfast();
            } else if (pos == 1) {
                chooseLunch();
            } else if (pos == 2) {
                chooseDinner();
            } else if (pos == 3) {
                chooseSnack();
            }

        }
        app_started = false;
    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub

    }

}
}

0 个答案:

没有答案