There is a RecyclerView for example with 3 positions, I find the maximum among all the positions and then I delete the maximum among the positions that I put and I find the maximum again and delete, and so on, in a word every 1 second should be deleted by 1 position

do{ Handler handler = new Handler(Looper.getMainLooper()); handler.postDelayed(new Runnable() { public void run() { int max = 0; int iOfMax = 0; for (int i = 0; i < arrayP.length; i++) { if (arrayP[i] > max) { max = arrayP[i]; iOfMax = i; } } dataModels.remove(iOfMax); adapter.notifyDataSetChanged(); } }, 4000); }while (--bundle>0); 

But for some reason it deletes everything completely in 4 seconds, what am I doing wrong?

    1 answer 1

    Because you create N tasks in a cycle and all of them (in the amount equal to the bundle variable) almost simultaneously run in 4 seconds. Remove the cycle and restart the task at the end of the task itself:

      Handler handler = new Handler(Looper.getMainLooper()); handler.postDelayed(new Runnable() { public void run() { int max = 0; int iOfMax = 0; for (int i = 0; i < arrayP.length; i++) { if (arrayP[i] > max) { max = arrayP[i]; iOfMax = i; } } dataModels.remove(iOfMax); adapter.notifyDataSetChanged(); if (dataModels.size() > 0) handler.postDelayed(this, 4000); } }, 4000);