如何将一个arraylist的某个参数显示到listview中

时间:2011-12-01 12:17:30

标签: android eclipse listview arraylist

我有一个列表视图中显示的数组列表。现在它可以工作,但它显示了一些随机数组ID查找名称,这就是为什么我希望它使用类的参数名称,这是一个字符串产品名称。

这是getters和setters文件,其中包含我要为列表显示的产品名称

   package kfc.project;

public class Product {


    String name;
    int servingSize;
    int calories;
    int fat;
    int saturatedFat;
    int transFat;
    int cholesterol;
    int sodium;
    int carbs;
    int fiber;
    int sugar;
    int protein;
    int vitamina;
    int vitaminc;
    int calcium;
    int iron;

    public Product (String name, int servingSize, int calories, int fat, int saturatedFat, int transFat, int cholesterol, int sodium, int carbs, int fiber, int sugar, int protein, int vitamina, int vitaminc, int calcium, int iron){
        this.name=name;
        this.calories=calories;
        this.servingSize=servingSize;
        this.fat=fat;
        this.saturatedFat=saturatedFat;
        this.transFat=transFat;
        this.cholesterol=cholesterol;
        this.fiber=fiber;
        this.sugar=sugar;
        this.sodium=sodium;
        this.carbs=carbs;
        this.protein=protein;
        this.vitamina=vitamina;
        this.vitaminc=vitaminc;
        this.calcium=calcium;
        this.iron=iron;
    }

    public Product (String name, int servingSize){
        this.name=name;
        this.servingSize=servingSize;
    }

    public String getName() {
        return name;
    }

    public int getServingSize() {
        return servingSize;
    }

    public int getCalories() {
        return calories;
    }

    public int getFat() {
        return fat;
    }

    public int getSaturatedFat() {
        return saturatedFat;
    }

    public int getTransFat() {
        return transFat;
    }

    public int getCholesterol() {
        return cholesterol;
    }

    public int getSodium() {
        return sodium;
    }

    public int getCarbs() {
        return carbs;
    }

    public int getProtein() {
        return protein;
    }

    public int getFiber(){
        return fiber;
    }

    public int getSugar(){
        return sugar;
    }
    public int getVitaminA(){
        return vitamina;
    }
    public int getVitaminC(){
        return vitaminc;
    }
    public int getCalcium(){
        return calcium;
    }
    public int getIron(){
        return iron;
    }

}

这是我添加产品的产品列表

package kfc.project;

import java.util.ArrayList;

公共类ProductList {     ArrayList列表;

public ProductList (){
    list = new ArrayList<Product>();

    //CREATE PRODUCT HERE
    Product chicken;
    list.add(new Product("Chicken", 10, 20, 30, 40, 50, 60, 70, 80, 90, 80,70,60,50,40,30));    

    Product rice;
    list.add(new Product("Rice",11));
}

public String[] getNames (){
    int c = 0;
    int size = list.size() - 1;
    String[] names = new String[size];

    while (size >= c) {
        //names.add(list.get(c).getName());
        names[c] = list.get(c).getName();
        c++;
    }

    c = 0;

    return names;
}

public ArrayList<Product> getList (){
    return list;
}

}

0 个答案:

没有答案