The buttons are not instantly dis-/reappearing and the code is very slow although i have no heavy operation running (skipping frames). I should use Threads but i do not understand how i should split my code in threads as i cant use normal thread to change the UI. For that there is the UIthread. But then i have to put the whole code into the UIthread as i have nothing else in my class and the same error would happen. Is Thread.sleep()
even used on UIthread?
How the user experience is:
Start button pressed. 20 sec pause. textview displays number
and the start button disappears and the stop button appears.
How it should be: First should the start button disappear after pressing and the stop button appearing. After that the textview should display x and update it so that it grows 0.1 each iteration until it reaches a random int or the user presses the stop button which change gamefalse[] to true which in turn stopps the while loop.
I redid my code but it still is lagging
Skipped 5885 frames!
final boolean[] gamefalse = {false};
int einsatz = 50;
final float[] x = {1};
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
start.setVisibility(View.GONE);
stop.setVisibility(View.VISIBLE);
SecureRandom random = new SecureRandom();
int number = random.nextInt(101);
int j = 1;
while (j < number && !gamefalse[0]) {
j++;
x[0] += 0.1;
textView.setText(String.valueOf(x[0]));
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println("iteration");
}
}
});
float finalX = x[0];
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
gamefalse[0] = true;
stop.setVisibility(View.GONE);
start.setVisibility(View.VISIBLE);
int gewinn = (int) finalX * einsatz;
mDatabaseReference.child("user").child(String.valueOf(encodeUserEmail(email))).child("stand").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
Long b = snapshot.getValue(Long.class);
mDatabaseReference.child("user").child(String.valueOf(encodeUserEmail(email))).child("stand").setValue(b + gewinn);
mDatabaseReference.child("user").child(String.valueOf(encodeUserEmail(email))).child("stand").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
Long b = snapshot.getValue(Long.class);
String c = b + " ";
System.out.println(c);
}
@Override
public void onCancelled(DatabaseError error) {
}
});
}
@Override
public void onCancelled(DatabaseError error) {
}
});
}
});
}