我有两个微调器,一个用户选择食物类型,另一个用于显示食物(例如早餐和粥)。然而,我的第二个旋转器没有展示我的食物。为什么会这样?下面是我的MainActivity类和我的Food类代码。
private void chooseBreakfast() {
planspinner1 = (Spinner) findViewById(R.id.planspinner);
List<Food> planlist = new ArrayList<Food>();
planlist.add(new Food("Porridge-35g/140cal",4));
planlist.add(new Food("Coffee-10g/10cal",1));
planlist.add(new Food("Toast-30g/90cal",3));
for(int i =0;i<planlist.size();i++){
System.out.println(planlist.get(i).getName()+ "has" + planlist.get(i).getCalories()+ "calories.");
}
ArrayAdapter<Food> planAdapter1 = new ArrayAdapter<Food>(this,android.R.layout.simple_spinner_item, planlist);
planAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
planspinner1.setAdapter(planAdapter1);
}
public class Food {
private String mstrName;
private int mintCalories;
public Food(String pstrName, int pintCalories) {
mstrName = pstrName;
mintCalories = pintCalories;
}
public String getName() {
return mstrName;
}
public int getCalories() {
return mintCalories;
}
}
为什么我的第二个微调器看起来像这样?
答案 0 :(得分:1)
覆盖Food类的toString()方法,它可能会有效。
public class Food {
private String mstrName;
private int mintCalories;
public Food(String pstrName, int pintCalories) {
mstrName = pstrName;
mintCalories = pintCalories;
}
public String getName() {
return mstrName;
}
public int getCalories() {
return mintCalories;
}
public String toString() {
return getName();
}
}