Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

292
Visualizações
Simple while-loop doesnt work

I got a little problem. I think the solution is very simple but unfortunately I can't find it. I hope someone can help me I got a while-loop who has to count up to ten and write the number into a TextView. It still doesn't work ... Thanks for your help! Here is the code:

package de.androidnewcomer.animation;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import static android.R.attr.button;
import static de.androidnewcomer.animation.R.id.textView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ImageView ball=(ImageView)findViewById(R.id.ball);
    Button button=(Button)findViewById(R.id.button);
    button.setOnClickListener(this);
}
@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.button:
            count();
            break;
    }
}
private void count() {
    TextView textView=(TextView)findViewById(R.id.textView);
    int i;
    i=1;
    while(i<10) {
        i++;
        textView.setText(i);
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
}
over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Using setText() with an integer value is for setting a string resource reference. To set the text itself, you have to provide a string: Use setText("" + i); and it should work.

over 4 years ago · Santiago Trujillo Relatório

0

The textView.setText(..) needs a string object, but you use a int. You have to convert your int into a string with the following possible options:

  1. You can use String.valueOf(i): textView.setText(String.valueOf(i));

  2. You can use Integer.toString(i): textView.setText(Integer.toString(i));

  3. You can use the empty string literal: textView.setText("" + i);

I prefer the last option. With your code, it should looks like the following code:

private void count() {
   TextView textView=(TextView)findViewById(R.id.textView);
   int i;
   i=1;
   while(i<10) {
       i++;
       textView.setText("" + i);
       try {
           Thread.sleep(200);
       } catch (InterruptedException e) {
           e.printStackTrace();
       }
   }
}

You can use a for loop instead of a while loop, like the following code:

private void count() {
   TextView textView=(TextView)findViewById(R.id.textView);

   for(int i = 1; i < 11; i++){ // From 1 to 10
       textView.setText("" + i);
       Thread.sleep(200); 
   }
}
over 4 years ago · Santiago Trujillo Relatório

0

Use a CountDown because you are blocking the main thread

CountDownTimer countDownTimer = new CountDownTimer(2000 /*amount*/, 200/*step*/) {
        public void onTick(long millisUntilFinished) {
           textView.setText("what ever you want");    
        }

        public void onFinish() {
            textView.setText("Done");
        }
    };
countDownTimer.start();
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda