In my android project, I have a countdown that randomizes a boolean for an array. The problem is, every time it ticks it always creates the array and the for loop. Can someone help me how to import my codes on a seperate class and method? I just want to call the returned value of an array in my countdown while it loops on a separate class. Please help me, TIA! 🙂
This is my countdown inside my onCreate():
new CountDownTimer(300000, 1000) {
public void onTick(long msUntilFinished) {
txtCounter.setText("" + msUntilFinished/1000);
ImageView[] pic= {img1, img2, img3, img4, img5};
Random aRandom = new Random();
for (int i=0;i<12;i++){
arrays[i] = aRandom.nextBoolean();
if(arrays[i]){
pic[i].setImageResource(R.drawable.show);
appear = true;
} else {
pic[i].setImageResource(R.drawable.hide);
appear = false;
}
}
} // end of onTick
public void onFinish() {
txtCounter.setText("done!");
}
}.start();
If you don’t want the array to be created each time it ticks just place the array initialization outside of the thread like this:
Important: “arrays” has to be final. If you cannot do this then you have to do: