我可以使用下面的代码来给适配器供电吗?如果是这样,任何想法如何造成我似乎都卡住了。如果没有,还有另一种方法来设置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应用程序构建的初学者。如果您足够友好地给我写答案,请尽量记住这一点!ty!
您应该使用枚举来构造数据。由于枚举是对象,它们可以具有属性和方法,因此可以设置一个具有ArrayList of times属性的VegetableTime枚举。
然后,您可以使用它,如果需要列表,则可以使用VegetableTime.values();来获取它们。
顺便说一句,“ Mildly”和co也应该是一个枚举(可以位于Time类中)。
代码示例,这是一种使用两个参数来定义什么是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;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句