0

Thanks so much for your help, the only problem is i changed the delay to 10000 because i have to display each question for 10 seconds but when i launch it the first question's display is delayed by 10 seconds as well and i don't want that to happen to the first question.. i need your help

'handler.postDelayed(new Runnable(){
        public void run(){
            int firstinteger = random.nextInt(100);
            int secondinteger = random.nextInt(100);
             int operator = rand.nextInt(4);
             String operand = "";
            //do something
            count++;
            switch (operator) {
                // cases = operator;

                case 0:

                    bothIntegers = firstinteger + secondinteger;
                    operand = "+";
                    break;

                case 1:
                    bothIntegers = firstinteger - secondinteger;
                    operand = "-";
                    break;

                case 2:
                    bothIntegers = firstinteger * secondinteger;
                    operand = "*";
                    break;

                case 3:
                    bothIntegers = firstinteger / secondinteger;
                    operand = "/";

                    break;
            }
            String result = firstinteger + "" + operand + "" + secondinteger + " = ";
            textView.setText(result.toString());
            handler.postDelayed(this, delay);
            if(count == 10){
                handler.removeCallbacksAndMessages(null);
            }
        }
    }, delay);'

1 Answers1

0

int count = 0;//global variable

final Handler handler = new Handler();
        final int delay = 5000; //milliseconds


        handler.postDelayed(new Runnable(){
            public void run(){
                //do something
                Log.e("XXX", String.valueOf(count));
                count = count + 1;
                if (count != 10) {
                    handler.postDelayed(this, delay);
                }
            }
        }, 0);

Hope to help you.

Tung Tran
  • 2,885
  • 2
  • 17
  • 24
  • i'm sorry i've posted a wrong version at the end can you review it please – Stackopolous Devorani Mar 01 '18 at 03:51
  • Add `String result = firstinteger + "" + operand + "" + secondinteger + " = "; textView.setText(result.toString());` into //do something – Tung Tran Mar 01 '18 at 03:52
  • Not use `for`, add count++ in `run()` method. when `count == 10`, you can stop `handler` – Tung Tran Mar 01 '18 at 03:59
  • 1
    Thanks so much for your help, the only problem is i changed the delay to 10000 because i have to display each question for 10 seconds but when i launch it the first question's display is delayed by 10 seconds as well and i don't want that to happen to the first question. – Stackopolous Devorani Mar 01 '18 at 04:23
  • `int count = 0;` final Handler handler = new Handler(); final int delay = 5000; //milliseconds handler.postDelayed(new Runnable(){ public void run(){ //do something Log.e("XXX", String.valueOf(count)); count = count + 1; if (count != 10) { handler.postDelayed(this, delay); } } }, 0); – Tung Tran Mar 01 '18 at 14:36