Hello, I have this loop that displays results: for (int i = 0; i < games.length; i++) { for (int j = 0; j < games.length; j++) { if (i == j | i > j) { continue; } else { counter++; System.out.println(counter + " " + games + " " + games[j]); } } } Now, i want the exact thing to write into a TextView when a button is pressed, so i made this into a onClick(View v) method : for (int i = 0; i < games.length; i++) { for (int j = 0; j < games.length; j++) { if (i == j | i > j) { continue; } else { counter++; result.setText(counter + " " + games + " " + games[j]); } } } Please help, I don't know how to show the same result into a TextView. Thank you
Will the following code snippet help? ((Button) findViewById(R.id.my_btn)).setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { TextView myTextView = (TextView) findViewById(R.id.my_textview); outer: for (int i = 0; i < games.length; i++) { for (int j = 0; j < games.length; j++) { if (i == j | i > j) { continue; } else { counter++; myTextView.setText(counter + " " + games + " " + games[j]); break outer; } } } });
Thanks for the tip, but it doesn't helped.. Below the whole program code: package com.thenewboston.bostjan; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class Bets extends Activity { EditText ekipa1, ekipa2, ekipa3; TextView result; String sEkipa1, sEkipa2, sEkipa3, data; Button izracunaj; int i, j, counter; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.bets); initialize(); final String games[] = { sEkipa1, sEkipa2, sEkipa3 }; izracunaj.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub konvertToString(); counter = 0; for (int i = 0; i < games.length; i++) { for (int j = 0; j < games.length; j++) { if (i == j | i > j) { continue; } else { counter++; result.setText(counter + " " + games + " " + games[j]); } } } } }); } public void initialize() { // TODO Auto-generated method stub ekipa1 = (EditText) findViewById(R.id.etEkipa1); ekipa2 = (EditText) findViewById(R.id.etEkipa2); ekipa3 = (EditText) findViewById(R.id.etEkipa3); izracunaj = (Button) findViewById(R.id.bIzracunaj); result = (TextView) findViewById(R.id.tvIzracun); } private void konvertToString() { // TODO Auto-generated method stub sEkipa1 = ekipa1.getText().toString(); sEkipa2 = ekipa2.getText().toString(); sEkipa3 = ekipa3.getText().toString(); } }
Its because you're using setText(), which replaces the current text with the given text. The solution is to create a StringBuilder before you begin your loops. While looping, append to the StringBuilder instead of using setText(). Once you have all the text in your StringBuilder, you simply call setText(stringBuilder.toString()). This will also be faster since you're only updating the textview once.