我可以使用此列表来提供Arrayadapter吗?

时间:2014-06-15 10:03:06

标签: android arrays adapter android-arrayadapter baseadapter

我可以使用以下代码为我的适配器供电吗?如果是这样,任何想法,我怎么会有点卡住。如果没有,是否有另一种方法来设置arraylist所以我每页有3个选项,而不必为每个食物制作一个新的类/ arraylist。 (如果这是唯一的方法,可能是大量的课程)

我将其设置为这样,因为每个按钮都有不同的时间值,可以将低谷放到下一页。

(例如)如果在上一页上,用户选择项目"朝鲜蓟"应该使用3个按钮加载一个新的页面,温和,中等和做得好(每个人的煮沸时间也不同)。

   package diederik.lucas.boiltime.data;
import java.util.ArrayList;
import java.util.List;

public class TimeOneData {


    private List<Time> Times = new ArrayList<Time>();
    public List<Time> getTimes() {
        return Times;       
    }

    //Not sure if void is the correct return value here
    public void asparagus() {
        addItem(new Time ("Mildly","6"));
        addItem(new Time ("Medium","8"));
        addItem(new Time ("Well Done","10"));
    }

    //Not sure if void is the correct return value here
    public void artichoke() {
        addItem(new Time ("Mildly","5"));
        addItem(new Time ("Medium","7"));
        addItem(new Time ("Well Done","9"));
    }


    //Not sure if void is the correct return value here
    public void beetroot() {
        addItem(new Time ("Mildly","7"));
        addItem(new Time ("Medium","10"));
        addItem(new Time ("Well Done","13"));
    }

    //Not sure if void is the correct return value here
    public void broadBeans() {
        addItem(new Time ("Mildly","1"));
        addItem(new Time ("Medium","2"));
        addItem(new Time ("Well Done","3"));

    }   //Not sure if void is the correct return value here
    public void broccolli() {
        addItem(new Time ("Mildly","4"));
        addItem(new Time ("Medium","8"));
        addItem(new Time ("Well Done","12"));

    }   //Not sure if void is the correct return value here
    public void cabbage() {
        addItem(new Time ("Mildly","3"));
        addItem(new Time ("Medium","6"));
        addItem(new Time ("Well Done","9"));
    }





    private void addItem(Time item) {
        Times.add(item);
    }


}

上下文:我是一个初学者,正在尝试学习Java + android app构建。如果你能给我一个答案,请尽量记住这一点! TY!

2 个答案:

答案 0 :(得分:0)

您应该使用Enum来构建数据。由于枚举是对象they can have attributes and methods,您可以设置一个具有ArrayList of times属性的VegetableTime枚举。

然后你可以使用它,如果你需要一个列表,你可以使用VegetableTime.values();

来获取它们

顺便说一句,“温和”&amp; co也应该是一个枚举(可以在时间类中)。

代码示例,这是一种使用它的两个参数定义什么是CookingTime的方法。

public class CookingTime {

    public enum Preparation {

        MILDLY, MEDIUM, WELL_DONE;
    }

    private int time;
    private Preparation preparation;

    CookingTime(Preparation preparation, int time){

        this.time = time;
        this.preparation = preparation;
    }
}

这是一个蔬菜,内联创建了这个烹饪的正确列表。

public enum Vegetable {

    ASPARGUS(Arrays.asList(new CookingTime(CookingTime.Preparation.MILDLY, 8), new CookingTime(CookingTime.Preparation.WELL_DONE, 15)));

    Vegetable(List<CookingTime> cookingTimes){
        this.cookingTimes = cookingTimes;
    }

    private List<CookingTime> cookingTimes;
}

答案 1 :(得分:0)

好的,这就是我想出来的:

Enum:  

public enum FoodTimeEnum {

ASPARAGUS ("2",  "3", "4"),
ARTICHOKE ("8",  "12", "16");
//more veggies


private final String mildly;
private final String medium;
private final String welldone;

private FoodTimeEnum(String mildly, String medium, String welldone) {
    this.mildly = mildly;
    this.medium = medium;
    this.welldone = welldone;
}

public String getMildly() {
    return mildly;
}

public String getMeduim() {
    return medium;
}

public String getWelldone(){
    return welldone;
}

}

和时间类

public class Time {
    public String timeName;
    public String timeMildly;
    public String timeMedium;
    public String timeLong;



    public Time(String id, String boilTime, String mildly, String medium, String welldone) { 
        this.timeName = id;
        this.timeMildly = mildly;
        this.timeMedium = medium;
        this.timeLong = welldone;

    }

    @Override
    public String toString() {
        return timeName;
    }

}