1. Download our Official Android App: Forums for Android!

Apps Noob question, help please..

Discussion in 'Android Development' started by gigen07, May 6, 2012.

  1. gigen07

    gigen07 Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    2
    Joined:
    May 6, 2012

    May 6, 2012
    2
    0
    5
    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
     

    Advertisement

  2. Shengxin

    Shengxin Well-Known Member
    Rank:
    None
    Points:
    58
    Posts:
    102
    Joined:
    Mar 16, 2012

    Mar 16, 2012
    102
    16
    58
    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;
    }
    }
    }
    });
     
  3. gigen07

    gigen07 Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    2
    Joined:
    May 6, 2012

    May 6, 2012
    2
    0
    5
    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();
    }


    }
     
  4. Unforgiven

    Unforgiven ...eschew obfuscation...
    Moderator
    Rank:
     #2
    Points:
    4,488
    Posts:
    38,408
    Joined:
    Jun 23, 2010

    Jun 23, 2010
    38,408
    47,778
    4,488
    Male
    Douglas, MA
    I've moved this to the Application Development forum so our coders can have a look at it.:)
     
  5. rektiphyr

    rektiphyr Newbie
    Rank:
    None
    Points:
    15
    Posts:
    19
    Joined:
    Mar 4, 2012

    Mar 4, 2012
    19
    0
    15
    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.
     

Share This Page

Loading...