Reputation: 29
The application have a loop to fill a Byte array (infinite times until a button press, but stopping it is working), and there are 6 (Image)Buttons in the activity. The first ImageButton should insert into the array 0, the second should 1, and so on.
The activity and the thread:
public class TestMap extends AppCompatActivity {
volatile private ImageButton a;
volatile private ImageButton b;
volatile private ImageButton c;
volatile private ImageButton d;
volatile private ImageButton e;
volatile private ImageButton f;
private boolean round0 = false;
private static volatile boolean stop = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
a = findViewById(R.id.flag_a);
b = findViewById(R.id.flag_b);
c = findViewById(R.id.flag_c);
d = findViewById(R.id.flag_d);
e = findViewById(R.id.flag_e);
f = findViewById(R.id.flag_f);
Button end = findViewById(R.id.endturn);
new Thread(new Runnable() {
public void run() {
while (!stop){
round0 = true;
for(int i = 0; i < squad_0.length; i++){
squad_0[i] = set_sqm0();
}
}
}
}).start();
round0 = false;
}
This stops the loop (and working):
public void endt(View view){
stop = true;
}
And the not working part:
public Byte set_sqm0() {
selected = 0; //default value
a.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
selected = 0;
}
});
//the same is for b-f, the selected value is 1-5
return selected;
}
The program fill the array with 0's without any button press, even if I delete the default value or edit a button's value to 6. Can someone help me how to pause the program until the user press the button?
Upvotes: 0
Views: 312
Reputation: 1167
First you need not and should not call a.setOnClickListener(...) in set_sqm0() because this executes it continuously during every pass through the while loop in your thread. Same goes for the initialization of selected = 0, which is also executed in each iteration of the while loop, reseting selected to 0 after you press the button. You should move the setOnClickListener() calls and the initialization of selected in onCreate(), before the creation of the thread.
Upvotes: 1