简体   繁体   English

我可以使用此列表来填充Arrayadapter吗?

[英]Can I use this list to feed an Arrayadapter?

Can I use the code below to feed my adapter? 我可以使用下面的代码来填充适配器吗? If so, any idea how cause I seem kinda stuck. 如果是这样,任何想法我似乎有点原因。 If not, is there another way to setup the arraylist so I do get 3 options per page without having to make a new class/arraylist for each food. 如果没有,是否有另一种方法可以设置arraylist,所以我每页确实有3个选项,而不必为每种食物制作新的类/ arraylist。 (might be a large amount of classes if thats the only way) (如果那是唯一的方法,则可能是大量的类)

I set it up like this because each button will have a different time value to put trough to the next page. 我之所以这样设置,是因为每个按钮将具有不同的时间值以将槽放入下一页。

(in example) If, on the previous page, user selects the item "artichoke" than a new page should load with the 3 buttons mildly, medium & well done (which will also have different boiling times each). (例如)在上一页中,如果用户选择“朝鲜蓟”项目,则新页面应轻柔,中等和良好(每个煮沸时间也不同)来加载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);
    }


}

Context: I am a beginner trying to learn Java + android app building. 上下文:我是一个尝试学习Java + android应用程序构建的初学者。 Please try to keep this in mind if you are kind enough to write me an answer! 如果您足够友好地给我写答案,请尽量记住这一点! ty! ty!

You should use an Enum to structure your data. 您应该使用枚举来构造数据。 As Enums are objects they can have attributes and methods , you can set up a VegetableTime enum having an ArrayList of times attribute. 由于枚举是对象, 它们可以具有属性和方法 ,因此可以设置一个具有ArrayList of times属性的VegetableTime枚举。

Then you can use that and if you need a list, you can get them by using VegetableTime.values(); 然后可以使用它,如果需要列表,则可以使用VegetableTime.values();获取它们。

By the way, "Mildly" & co should also be an enum (that could sit in Time class). 顺便说一句,“轻度”和co也应该是一个枚举(可以位于Time类中)。

Code example, here's a way to define what is a CookingTime with it's two parameters. 代码示例,这是一种使用两个参数来定义什么是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;
    }
}

And here's a Vegetable with inline creation of the right list for this cooking. 这是一种蔬菜,可以在线创建此烹饪的正确清单。

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

Ok this is what I came up with: 好的,这就是我想出的:

Enum:  

public enum FoodTimeEnum{ 公共枚举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;
}

} }

and in the Timeclass 在时间课上

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

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何使用arrayadapter? - How I can use an arrayadapter? 如何使列表线程安全,以便与自定义ArrayAdapter一起使用? - How can I make my list thread safe in order to use it with a custom ArrayAdapter? 如何使用ArrayAdapter显示列表视图? - How do I use ArrayAdapter to display a list view? 单击后如何在arrayadapter列表中打开文件? - How can I open the file in arrayadapter list after clicking? 我可以在两个不同的listView中使用相同的ArrayAdapter吗? 怎么样? - Can I use the same ArrayAdapter in two different listViews? How? 如何在带有自定义ArrayAdapter的ListView中使用ViewPropertyAnimator? - How can I use ViewPropertyAnimator in a ListView with a custom ArrayAdapter? 我将ArrayAdapter与GridView中的列表一起使用。 如何配置基于ArrayAdapter中对象变量的排序? - I am using ArrayAdapter with a List in a GridView. How can I configure to sort based on object variables from my ArrayAdapter? 语音识别,ListView ArrayAdapter:使用 if(&quot;certain word speak&quot;){ } 的最佳方法是什么?我可以使用哪些方法? - Speech Recognition, ListView ArrayAdapter: What is the best way to use if("certain word spoken"){ } is there methods i can use? 如何在Android的ArrayAdapter的单行中从对象列表中提取特定对象? - How can I extract a specific object from a list of objects in a single row of an ArrayAdapter in Android? 在初始化 ArrayAdapter 时,我可以使用变量来代替调用数组的第二个参数的最后一部分吗? - Can I use a variable to substitute for the last part of the second argument that calls an array when initializing an ArrayAdapter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM