Answer the question
In order to leave comments, you need to log in
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()) + "' элементам");
}
}
Colors colors;
and I'm trying to reach the arraySystem.out.println ("Размер массива равен '" + Integer.valueOf (colors.colorsArray.size()) + "' элементам");
the application crashes. Colors colors = new Colors();
Answer the question
In order to leave comments, you need to log in
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.
and it's hard for me to switch from iOS to Android
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 questionAsk a Question
731 491 924 answers to any question