将整数值添加到我的数组

时间:2015-02-10 10:28:24

标签: java android android-studio

这是我的早餐旋转器,我可以在阵列中选择我可能的早餐...我将如何添加功能以显示用户所选食物的卡路里。

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

1 个答案:

答案 0 :(得分:0)

您可以创建一个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