'Transfer an ArrayList<Character> or <Integer> from one class to the activity class
I'm trying to transfer this code from one class to one of my activity classes in Android Studio as I am trying to use it for my Text View.
public class cartesian() {
public ArrayList<Character> list1() {
Random rand = new Random();
int low = 3;
int high = 5;
int result = rand.nextInt(high - low) + low;
ArrayList<Character> list1 = new ArrayList<>(result);
for (int i = 0; i <= result; i++) {
list1.add((char)(rand.nextInt(26) + 'a'));
}
return list1;
}
public ArrayList<Integer> list2() {
Random rand = new Random();
int low = 3;
int high = 5;
int result = rand.nextInt(high - low) + low;
ArrayList<Integer> list2 = new ArrayList<>(result);
for (int i = 0; i <= result; i++) {
list2.add(rand.nextInt(100));
}
return list2;
}
}
I have tried one of the answers from here, the one by "bastien pinaquy" but that didn't work either as it returns null each time. I also tried using a custom object but I'm not confident in my understanding of it to allow myself to make much use of it.
Custom Object Class
public class CustomObject {
private List<Character> charList;
private List<Integer> intList;
public CustomObject(){
// not sure if I need to put anything in here
}
public List<Character> getCharList() { return charList; }
public void setCharList(List<Character> charList) { this.charList = charList; }
public List<Integer> getIntList() { return intList; }
public void setIntList(List<Integer> intList) { this.intList = intList; }
}
Activity Class
public class cartProdActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cart_prod);
ArrayList<Character> charList = list1();
String charListStr = charList.toString();
ArrayList<Integer> intList = list2();
String intListStr = intList.toString();
setTxt(charListStr, intListStr);
}
public void setTxt(String list1, String list2) {
TextView list1View = (TextView) findViewById(R.id.list1);
list1View.setText(list1);
TextView list2View = (TextView) findViewById(R.id.list2);
list2View.setText(list2);
}
// the list1 and list2 functions refer to the functions from the first code block
// they are there as I was testing whether the code was working or not
The function itself works as I have put it in the activity class to check but I want it in a separate class I need to work with the list itself for other purposes.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
