• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps integer array to textview

roll

Lurker
how to write integer array to textview,what i tried is
int​
[] s = getIntent().getIntArrayExtra("ar");

for(int i=0;i<s.length;i++)

chars = (
" "+s).toCharArray();



String se = String.valueOf(chars);
tv.setText(se);
need a quick help
thank:)s in advance
 
Here is a sample you can refer to:

[HIGH]int[] intArray = {1,2,3,4,5};
StringBuilder sb = new StringBuilder();
for(int i : intArray) {
sb.append(Integer.toString(i));
}
String intString = sb.toString();[/HIGH]
 
still prob is it is not printed on textview

Then you likely have a problem with the display logic, you would need to post the code (XML layout) and how you get a reference to the TextView.


Are you sure the TextView is visible? (give it a strange background color in the XML to test this)


shredcode's code snippet is correct -- and the fastest, recommended way for looping over a collection and building a string.
 
I'm using 2 file 1 to get data that i'm pass'n to another as
x.java
in.putExtra("ss", fs); where fs is an integer array
y.java
int yy [] = getIntent().getExtras().getIntArray("ss");
String sop="hi";

StringBuilder sb=​
new StringBuilder();
for(int i: yy){
sb.append(Integer.toString(i));

}
String str = sop+sb.toString();
tv.setText(str);//where tv is textview which is visible i have checked


 
umm... since the text view is visible, is there a chance that the integer array might be empty or something along those lines, so that even when you build the string it still comes up blank??
 
I'm using 2 file 1 to get data that i'm pass'n to another as
x.java
in.putExtra("ss", fs); where fs is an integer array
y.java
int yy [] = getIntent().getExtras().getIntArray("ss");
String sop="hi";

StringBuilder sb=​
new StringBuilder();
for(int i: yy){
sb.append(Integer.toString(i));

}
String str = sop+sb.toString();
tv.setText(str);//where tv is textview which is visible i have checked




What does this display? Do you see the "hi" part? Can you post a screenshot with the background color changed on the TextView?

( For me, 9 times out of 10 the issue is that some layout is hiding my textview :o )


Also, sorry I missed this before but you should use String.valueOf(i); not Integer.toString(i);


As far as I can tell the
Integer class does not have a toString method with 0 parameters, so I would be surprised if that compiled.

Nevermind this, it does i was looking at the javadocs with very tired eyes



 

As far as I can tell the
Integer class does not have a toString method with 0 parameters, so I would be surprised if that compiled.

The parameter is "i", an integer. One problem here is that shredcode's example has a mistake. It should be: sb.append(Integer.toString(yy)); and not sb.append(Integer.toString(i));


Otherwise, this it will just print the integers 0,1,2.. instead of the actual integers contained in yy[]. Also, there are no delimiters in the code, so they will also just print 012.. with no spaces. Can the OP specify how he wants it formatted?
 
The parameter is "i", an integer. One problem here is that shredcode's example has a mistake. It should be: sb.append(Integer.toString(yy)); and not sb.append(Integer.toString(i));


Otherwise, this it will just print the integers 0,1,2.. instead of the actual integers contained in yy[]. Also, there are no delimiters in the code, so they will also just print 012.. with no spaces. Can the OP specify how he wants it formatted?


I do want to point out the difference and confusion I think you are having with the different 'for' loop options in java.

Java 5 brought about the 'for each' method:

for(int i : intArray){
//some code
}

Which will iterate over each position, giving you access to the value of each position as the variable 'i' in my above example.

Original style:

for(int x = 0; x < intArray.length; x++){
//some code
}

This gives you access to the array position, and you would then access the integer using that index such as 'intArray[x]'. In his example he would be fine using the 'for each' method and my original example is correct. yy will not give you the expected results you intended.

I was just providing the actual syntax and a sample for iterating and converting an int to a String within a loop.

I do agree that the expected format is needed to provide a complete solution, but i think the data that we all have provided is enough to solve this problem.

The problem is not with the looping. It is with the actual setup of the view. In his example he should have at least seen the text 'hi' within his textView. This is because he initialized the String to 'hi' and if the int array was an array size 0 it would have skipped the loop and just combined hi to "".

Since 'hi' did not show in the textView, it lets us know it is a view setup issue and not a loop issue.
 
I do want to point out the difference and confusion I think you are having with the different 'for' loop options in java.

Java 5 brought about the 'for each' method:

for(int i : intArray){
//some code
}
Indeed this is correct, I dont know why I read the dev forums when sleepy :)

Since 'hi' did not show in the textView, it lets us know it is a view setup issue and not a loop issue.
Yeah that's what I asked earlier -- I'm not sure if the OP said 'hi' doesn't show, though.

But I agree, if 'hi' wasn't showing it's definitely the layout or display code.
 
The parameter is "i", an integer. One problem here is that shredcode's example has a mistake. It should be: sb.append(Integer.toString(yy)); and not sb.append(Integer.toString(i));


Otherwise, this it will just print the integers 0,1,2.. instead of the actual integers contained in yy[]. Also, there are no delimiters in the code, so they will also just print 012.. with no spaces. Can the OP specify how he wants it formatted?



As shredcode said, this is actually incorrect and could throw a very evil ArrayIndexOutOfBoundsException.


As an aside, this is a lesson (for us all) in not naming variables things like "yy" :)
 
Back
Top Bottom