2015-02-10 102 views
0

我有兩個旋轉器,一個用戶選擇食物類型,另一個用來顯示食物(例如早餐和粥)。然而,我的第二個微調不顯示我的食物。爲什麼會出現?以下是我的MainActivity類和我的食物類代碼。Spinner not display entries

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

爲什麼我的第二個微調框會出現這樣? Why would my second spinner appear like this

回答

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

} 
+0

這工作完美,非常感謝! – NicolaBA 2015-02-10 12:25:30

+0

如果它工作,標記它正確和upvote將是偉大的。 :d – 2015-02-10 12:27:37