2

I am trying to add Arraylist to Text View But it gives an error while adding it to TextView Declaration of Arraylist is:

    ArrayList<Integer> a=new ArrayList<Integer>();


    1.       int arraylistSize = a.size();

    2.       for(int i = 0; i < arraylistSize; i++){

    3.  textview1.setText(a[i]);

    4.  textview1.setText("*");

At line 3 it gives "The type of the expression must be an array type but it resolved to ArrayList"

Pete B.
  • 3,188
  • 6
  • 25
  • 38
Sam
  • 97
  • 1
  • 14

5 Answers5

4

a textview1.setText(a[i]); => textview1.setText(a.get(i));

or something like:

for(Integer i : a) textview1.setText(i);

I don't see how this will produce any kind of desired functionality, line 4 will override line 3 and each iteration of line 3 will override the previous one. After the loop executes you will end up with * in the textview1.

Pete B.
  • 3,188
  • 6
  • 25
  • 38
3

You will want to use ArrayList's get(index) operation. i.e. a.get(i).

If you are looking to see 1*2*3*4..etc.. printed out as text in the textview. you will want to append all these integers together into one string. Example:

String s = "";
for (int i = 0; i < a.size(); i++) {
     s += a.get(i) + "*";
}
textview1.setText(s);
josh527
  • 6,971
  • 1
  • 19
  • 19
0

You could use the append method in textview

for (int i=0;i<a.size();i++){
textview1.append(a.get(i) + "*");
}
Navneet Krishna
  • 5,009
  • 5
  • 25
  • 44
0
ArrayList<Integer> a = new ArrayList<Integer>();
for(String i : a)
   textview1.append(i + "\n\n");
Abhi
  • 1,127
  • 1
  • 12
  • 25
-1

You can use the short hand method of iterating over a list as such:

ArrayList<Integer> a = new ArrayList<Integer>();
for (Integer i : a) {
    textview1.setText(Integer.toString(i) + "*");
}
Community
  • 1
  • 1
TronicZomB
  • 8,667
  • 7
  • 35
  • 50