Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

293
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda