Привет! Я использую поток, чтобы установить таймер, но есть некоторые проблемы, чтобы остановить его.
1. Как я могу остановить таймер, когда значение textView равно 0.
2. Когда я получаю данные из firebase, я хочу, чтобы таймер запускался снова каждый раз, когда вопрос обновляется.
thread.stop или thread.interpt не работают.
thread = new Thread(){
@Override
public void run() {
while (!interrupted()){
try {
count--;
Thread.sleep(1000);
runOnUiThread(new Runnable() {
@Override
public void run() {
txt_timer.setText(String.valueOf(count));
if(count==0){
Toast.makeText(Questions.this, "No Answer Selected", Toast.LENGTH_SHORT).show();
}
}
});
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
Вот моя функция для обновления вопроса
private void updateQuestion() {
QuestionRef.child(newId).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
int numberOfQuestions = (int) dataSnapshot.getChildrenCount();
if (numberOfQuestions > id) {
thread.start();
String question =dataSnapshot.child("Question").getValue(String.class);