0

I am trying to implement a delay on button click, each second I need to remove one item from a list called solved_cells, the list initially has 16 items. below is what I did:

solve_all.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

                   while (solved_cells.size()>=1) {
                       v.postDelayed(new Runnable() {
                           @Override
                           public void run() {
                               //Execute code here
                        solved_cells.remove(solved_cells.size() - 1);
                          }
                       }, 1000L);

                    }
        }
    });

but each time I debug , I see the code reach to :

v.postDelayed(new Runnable() {

and doesn't touch the public void run() { function, Your suggestions are highly appreciated.

Faysal Ahmed
  • 7,501
  • 5
  • 28
  • 50
  • `postDelayed` just puts the `Runnable` into the queue for later execution, it does not run it immediately. This turns your `while` into an endless loop. – Henry Jan 14 '18 at 08:05

1 Answers1

0

i don't know exactly what your problem in your code but you can use timer instead like this by creating TimerTask class

   Timer timer;
   MyTimerTask timerTask;
private class MyTimerTask extends TimerTask {

    @Override
    public void run() {
        try {
            activity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if (solved_cells.size() > 0) {
                        solved_cells.remove(solved_cells.size() - 1);
                    } else {

                        timer.cancel();
                        timer.purge();
                    }
                }
            });
        } catch (Exception e) {
            // e.printStackTrace();
        }
    }
}

and in your listener you can declare it like this

solve_all.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
      timerTask = new MyTimerTask();
      Timer t = new Timer();
      t.schedule(timerTask , 1000L);

    }
});
Mosa
  • 353
  • 2
  • 16
  • Thanks, but the same problem. your code removes only one item from the list. when implementing while loop like this 'public void onClick(View v) {while (solved_cells.size() >= 1) { timerTask = new MyTimerTask(); Timer t = new Timer(); t.schedule(timerTask , 1000L); }}' then the 'run()' function is not called. – صلي علي محمد - Atef Farouk Jan 14 '18 at 08:38
  • I need to remove all items, one by one each second. – صلي علي محمد - Atef Farouk Jan 14 '18 at 08:40
  • Thanks Mosa, I edited your answer to call the timer and it's working now. ' Override public void run() { if (Game.solved_cells.size() > 0) { int yyy = cells.get(solved_cells.get(solved_cells.size() - 1)); Game.solved_cells.remove(Game.solved_cells.size() - 1); timerTask = new MyTimerTask(); Timer t = new Timer(); t.schedule(timerTask , 1000L);' – صلي علي محمد - Atef Farouk Jan 14 '18 at 08:48
  • this must remove one item each second until there is no items – Mosa Jan 14 '18 at 08:48
  • ok am glad this solution work for you, if it worked fine pleaswe accept the answer and vote up – Mosa Jan 14 '18 at 08:51