Reputation: 33
I can't get String Builder to work properly for me, I know the values generated work but only the last one is added into the textView.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
Intent newGame = getIntent();
int temp = newGame.getIntExtra("int_value", 0); // here 0 is the default value
Random r = new Random();
int Low = 1;
int High = 4;
int Result = r.nextInt(High-Low) +Low;
int[] numbers = new int [1];
for(int Generated = 0; Generated < numbers.length; Generated++) {
numbers[Generated] = (int)(Math.random() +temp);
}
int value;
for (int rolls = 0; rolls < 4 +temp; rolls++) {
value = (int) (Math.random() * 4 + 1);
//Test if numbers generated are correct
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
StringBuilder builder = new StringBuilder();
builder.append(value + " ");
textView.setText(builder.toString());
// Set the text view as the activity layout
setContentView(textView);
//store generated numbers here
System.out.println(value);
}
}
In System.out.println(value); I can see the numbers generated but the textView only shows the last one.
Upvotes: 2
Views: 2535
Reputation: 129
So, what you want to do is use the StringBuilder to build up your string in the loop. Then set the value of the StringBuilder to the textView after the loop. Like this_:
StringBuilder builder = new StringBuilder();
for (int rolls = 0; rolls < 4 +temp; rolls++) {
value = (int) (Math.random() * 4 + 1);
builder.append(value + " ");
System.out.println(value);
}
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(builder.toString());
// Set the text view as the activity layout
setContentView(textView);
Upvotes: 0
Reputation: 2391
Try edit like this:
StringBuilder builder = new StringBuilder();
for (int rolls = 0; rolls < 4 +temp; rolls++) {
value = (int) (Math.random() * 4 + 1);
//Test if numbers generated are correct
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
builder.append(value + " ");
textView.setText(builder.toString());
// Set the text view as the activity layout
setContentView(textView);
//store generated numbers here
System.out.println(value);
}
Upvotes: 2