Search code examples
androidarraysadapterandroid-arrayadapterbaseadapter

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. (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).

   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. Please try to keep this in mind if you are kind enough to write me an answer! ty!


Solution

  • 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.

    Then you can use that and if you need a list, you can get them by using VegetableTime.values();

    By the way, "Mildly" & co should also be an enum (that could sit in Time class).

    Code example, here's a way to define what is a CookingTime with it's two parameters.

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