0

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);
1
  • It only displays one element because that is all you are accessing. Commented Nov 16, 2012 at 2:42

4 Answers 4

2
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);
Sign up to request clarification or add additional context in comments.

Comments

1

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);

6 Comments

but I put it to where the numbers are random? which is x. Sorry new to android and coding
I don't know what x is, it's defined in your code. You're currently showing the xth item using .get(x). The above will show all elements in the array.
but isn't your code going to show all 100 number if the max is 100. Im only trying to pull five numbers out of 100. any idea? I found a way but every time I click the button again it doesnt reset the value and prints another set of numbers right next to the prevoius one
@marcusmofo Ooooooooooookay, I finally get your problem. :) I've updated my answer again.
Problem Solve wow thanks man. I will eventually be on your level of coding. thanks alot
|
0

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.

2 Comments

so something like this. String num = (" " + x); then use the method I was using ?
say you have an array of 3 numbers e.g. al = [1, 2, 3]. if you call al.get(0), it will return the first number from the array -- 1. so String num = (" " + x); will not produce the result you are looking for. What you would need to do for the above example is String num = al.get(0) + " " + al.get(1) + " " + al.get(2)... and so on. A for loop is suitable for this purpose. e.g. String num = ""; for(int x=0;x<al.length;x++) { num += " " + al.get(x); } nameoffile.setText(num);
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.