将整数值添加到我的数组

Adding integer values to my array

这是我的早餐微调器,在数组中选择了我可能的早餐...我将如何添加功能以显示用户所选食物的卡路里。

 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);

您可以创建一个 Food.java 对象:

class Food{
  String name;
  int calories;
  // getters and setters
  public String toString(){
     return name + " (" + calories + ")"; // return your description here
  }
}

然后创建食物列表:

List<Food> planlist = new ArrayList<Food>();
planlist.add(new Food("Cereal", 20));
planlist.add(new Food("Porrige", 25));
planlist.add(new Food("Fruit", 10));

ArrayAdapter<Food> planAdapter1 = new ArrayAdapter<Food>(this, android.R.layout.simple_spinner_item, planlist);
planAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(planAdapter1);

this answer.