Print String[] In textView on Android - android

I've a String[] and i'd like print all positions this String in textview
code:
for (int i = 0; i < something.length; i++) {
myMsg.setText(something[i]);
}
but in textview, only last position is being displayed.

because you are replacing your text in your textview at every iteration by calling setText.
use append instead. like this:
for (int i = 0; i < something.length; i++) {
myMsg.append(something[i]);
}

if you don't want to use append for any reason. You can use Concatenation Operator +.
myMsg = ""; // initialize String to to empty
for (int i =0 ; i< something.length ; i++){
myMsg+= something[i];
}

You can try this too, If you dont want to use loop.
myMsg.setText(Arrays.toString(something));
or without commas and brackets
myMsg.setText(Arrays.toString(something).replaceAll("[,\\[\\]]+", " "));
or without commas and brackets and removed trailing and leading whitespace
myMsg.setText(Arrays.toString(something).replaceAll("[,\\[\\]]+", " ").trim());
Hope it helps :)

Related

How to get counts of characters on every line in TextView?

My TextView has multi-line text. And I want to get counts of characters every single line. I have tried String.split("\n"), but it didn't work...
Try to splite by line using below code
String lines[] = String.split("\\r?\\n");
OR
String lines[] = String.split(System.getProperty("line.separator"));
and If you don’t want empty lines:
String lines[] = String.split("[\\r\\n]+")
Try this:
String[] lines = String.split(System.getProperty("line.separator"));
int[] counts = new int[lines.length];
for(int i = 0; i < lines.length; i++ ){
String line= lines[i].replace(" ", "").trim();
counts[i] = line.toCharArray().length;
}
Then you will get every line's counts of character even you just have one line or empty lines.
You should create a function which store each line in different String variable and then count that string length.

Converting an ArrayAdapter to a List<String>

I have an array adapter(string), and would like to convert it to a List<String>, but after a little googling and a few attempts, I am no closer to figuring out how to do this.
I have tried the following;
for(int i = 0; i < adapter./*what?*/; i++){
//get each item and add it to the list
}
but this doesn't work because there appears to be no adapter.length or adapter.size() method or variable.
I then tried this type of for loop
for (String s: adapter){
//add s to the list
}
but adapter can't be used in a foreach loop.
Then I did some googling for a method (in Arrays) that converts from an adapter to a list, but found nothing.
What is the best way to do this? Is it even possible?
for(int i = 0; i < adapter.getCount(); i++){
String str = (String)adapter.getItem(i);
}
Try this
// Note to the clown who attempted to edit this code.
// this is an input parameter to this code.
ArrayAdapter blammo;
List<String> kapow = new LinkedList<String>(); // ArrayList if you prefer.
for (int index = 0; index < blammo.getCount(); ++index)
{
String value = (String)blammo.getItem(index);
// Option 2: String value = (blammo.getItem(index)).toString();
kapow.add(value);
}
// kapow is a List<String> that contains each element in the blammo ArrayAdapter.
Use option 2 if the elements of the ArrayAdapter are not Strings.

Android: Fill array up to a certain length

The situation
I have got different char[] arrays with a different amount of items. To avoid the "OutOfBounds"-Error while processing them I want to standardize them.
For Example:
My Array has following items: "5;9" --> What I want it to look like: "0,0,0,0,5,9" (fill up with "0"s to 6 items)
What I tried:
char[] myarray1 = mystring1.toCharArray();
...
for(int i=0; i<6; i++){
myarray1[i] = 0;
if(i<myarray1.length-1){
myarray1[i] = myarray1[i];
}else{
myarray1[i] = 0;
};
};
My code failed, because it evokes exactly that error...
I hope somebody can help me :)
Thanks!
The reason why your solution doesn't work is that you are using the same array for everything.
After char[] myarray1 = mystring1.toCharArray(); the length of myarray1 is 2, so you cannot simply assign entry 2,3,4 and 5 in your loop. Furthermore if you want the character ´0´ to be in the string, you need to surround your zeros with apostrophes.
You can fix your solution like this:
char[] myNewArray = new char[6];
int myarrayIndex = 0;
for(int i=0; i<6; i++)
{
if(i < (myNewArray.length - myarray1.length)) {
myNewArray[i] = '0';
} else {
myNewArray[i] = myarray1[myarrayIndex++];
};
};
System.out.println(myNewArray); //Will print out: 000059
An easier solution could be this:
myarray1 = String.format("%6s", mystring1).replace(' ', '0').toCharArray();
Firstly trying to fill 0's is not going to fix the error.
Secondly your logic is not right(assuming size as 6), change it to myString.length().
And I don't understand the point of myarray1[i] = myarray1[i];.
Anyways, every array with integer size is initialized by Zero's according to Java specs. On the other hand if you want to fill it with any other value, try Arrays.fill().
I think this function will accomplish what you're trying to do.
private static String formatString(String input)
{
String FORMATTED_STRING = "0,0,0,0,0,0";
int difference = FORMATTED_STRING.length() - input.length();
return FORMATTED_STRING.substring(0, difference) + input;
}

Array integer Android

ok so i create an array that has integers. The array displays five number from the min and max. How can i display all five numbers in a textview or edittext ? I tried:
nameofile.setText(al.get(x).toString());
but it only displays one?
ArrayList<Integer> al = new ArrayList<Integer>();
for (int i = minint; i <= maxint; i++)
al.add(i);
Random ran = new Random();
for (int i = 0; i < 5; i++) {
int x = al.remove(ran.nextInt(al.size()));
String myString = TextUtils.join(", ", al);
lottonumbers.setText(myString);
ArrayList<Integer> al = new ArrayList<Integer>();
al.add(0);
al.add(1);
al.add(5);
al.add(4);
al.add(3);
java.util.Collections.sort(al);//for sorting Integer values
String listString = "";
for (int s : al)
{
listString += s + " ";
}
nameofile.setText(listString);
You're currently only printing out one element (the one at index x). If you want to print them all in order, you can just join them using TextUtils.join().
Update: After seeing your edit, I think there's a better way to go about what you're trying to do. Instead of trying to pull the values one at a time, and update the list, why not just shuffle them, then use the above method?
Update 2: Okay, I think I finally understand your problem. Just a simple change, then.
ArrayList<Integer> al = new ArrayList<Integer>();
for (int i = minint; i <= maxint; i++)
al.add(i);
Random ran = new Random();
StringBuilder text = new StringBuilder(); // Create a builder
for (int i = 0; i < 5; i++) {
int x = al.remove(ran.nextInt(al.size()));
if (i > 0)
text.append(", "); // Add a comma if we're not at the start
text.append(x);
}
lottonumbers.setText(text);
al.get(x).toString() will only get the value at index "x". If you want to display all values, you need to combine all of the values from the array into a single string then use setText on that string.
You are only showing one number of your array in the TextView, you must to concat the numbers to see the others results like:
for(Integer integer : al) {
nameofile.setText(nameofile.getText() + " " + al.get(x).toString());
}
Then i think you can show all number in one String.

Android how to print a array in text view or anything

I have a array of words and I would like to print the array of words onto the screen in a text view after a button is clicked. i was using a for loop to go through the whole list and then set text, but i keep getting a error with that plus it will just replace the last value, so it wont print the whole array. If anybody could explain to me how to do like a g.drawSting() but android version that would be great. my code rt now is not with me but it something like:
-I'm a beginner to android btw, probably could tell by this question tho.
public void onCreate(Bundle savedInstanceState)
{
//code for a button just being pressed{
//goes to two methods to fix the private array{
for(int y=0; y<=array.size()-1; y++){
textArea.setText(aarray.get(y)); //prints all strings in the array
}
}
}
int arraySize = myArray.size();
for(int i = 0; i < arraySize; i++) {
myTextView.append(myArray[i]);
}
if you want to print one by one, then use \n
myTextView.append(myArray[i]);
myTextView.append("\n");
PS:
Whoever suggesting to change .size() to .length(), thanks for you suggestion.
FYI,
The questioner mentioned the variable name is array.size() in question, so the answer also having the same variable name, to make it easier for the questioner.
if your variable (myArray) is an Array use myArray.length(), if it is ArrayList use myArray.size()
You have to combine all text into a String before you can give it the TextView. Otherwise you overwrite the text all the time.
public void onCreate(Bundle savedInstanceState)
{
StringBuilder sb = new StringBuilder();
int size = array.size();
boolean appendSeparator = false;
for(int y=0; y < size; y++){
if (appendSeparator)
sb.append(','); // a comma
appendSeparator = true;
sb.append(array.get(y));
}
textArea.setText(sb.toString());
}
I use this no-index solution, just an easy to remember one liner:
for(File file:list) Log.d(TAG, "list: " + file.getPath());

Categories

Resources