S
S
Stanislav Korolevskiy2017-10-25 09:09:12
Java
Stanislav Korolevskiy, 2017-10-25 09:09:12

Can't access class from activity?

In the previous question, they helped me a lot with the competent creation of an ArrayList (sincere thanks for this). Here are its modified construction and class construction (questions under the code):

import java.util.ArrayList;
import java.util.List;

public class Colors {

    String name;
    Integer r;
    Integer g;
    Integer b;
    String hex;
    String cmyk;
    Boolean luminous;
    Boolean metallic;

    public Colors (String name, Integer r, Integer g, Integer b, String hex, String cmyk, Boolean luminous, Boolean metallic) {
        setName(name);
        setR(r);
        setG(g);
        setB(b);
        setHex(hex);
        setCmyk(cmyk);
        setLuminous(luminous);
        setMetallic(metallic);
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setR(Integer r) {
        this.r = r;
    }

    public Integer getR() {
        return r;
    }

    public void setG(Integer g) {
        this.g = g;
    }

    public Integer getG() {
        return g;
    }

    public void setB(Integer b) {
        this.b = b;
    }

    public Integer getB() {
        return b;
    }

    public void setHex(String hex) {
        this.hex = hex;
    }

    public String getHex() {
        return hex;
    }

    public void setCmyk(String cmyk) {
        this.cmyk = cmyk;
    }

    public String getCmyk() {
        return cmyk;
    }

    public void setLuminous(Boolean luminous) {
        this.luminous = luminous;
    }

    public Boolean getLuminous() {
        return luminous;
    }

    public void setMetallic(Boolean metallic) {
        this.metallic = metallic;
    }

    public Boolean getMetallic() {
        return metallic;
    }

    List<Colors> colorsArray = new ArrayList<>();

    public void addColors() {
        colorsArray.add(new Colors("1000", 205, 186, 136, "#cdba88", "5, 10, 40, 10", false, false));
        colorsArray.add(new Colors("1001", 208, 176, 132, "#d0b084", "5, 20, 40, 10", false, false));
        colorsArray.add(new Colors("1002", 210, 170, 109, "#d2aa6d", "5, 20, 50, 10", false, false));

        System.out.println ("Размер массива равен '" + Integer.valueOf (colorsArray.size()) + "' элементам");
    }

}

Problems and questions:
1. In an activity I create a variable with data type Colors. Colors colors;and I'm trying to reach the array
System.out.println ("Размер массива равен '" + Integer.valueOf (colors.colorsArray.size()) + "' элементам");
the application crashes.
2. I can't create an instance of the class either: 3. Can an array be filled in advance without add()? 4. I will fill listview with data from array. How to access elements by their keys name, r, g etc.? I apologize for maybe stupid questions, but the project is on fire, and it's hard for me to switch from iOS to Android... Colors colors = new Colors();

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Andrey, 2017-10-27
@korolevsky_s

You have in colorsArray - objects of class Colors. When accessing by index from this list, you get an object, and then do whatever you need with it - you have getters / setters for all fields.

M
Maxim Moseychuk, 2017-10-25
@fshp

and it's hard for me to switch from iOS to Android

And what's so difficult? All the same. You have a constructor in a class that has parameters.
And you are trying to create an object through a constructor without parameters, you do not have such a constructor.
Farther. To access "colorsArray" it must be declared as public. Or write a getter for it.
This is a reference to null.
Any person who has worked with OOP and has never seen java should understand such simple things regardless of the language.

S
Stanislav Korolevskiy, 2017-10-25
@korolevsky_s

Like with accessibility of a class understood. On the advice, I wrote a getter for colorsArray, I got access to colorsArray. But how to access even deeper. Let's say I need to assign the value name to a variable at index 5. I can only work with the colorsArray index.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question