我正在尝试制作一个测验android应用程序,其中我要实现的是向用户显示倒数,直到测验开始,并且当倒数计数为零时,启动开始按钮。
最初有一个测验,倒计时为60秒(即60秒后,应激活测验的开始按钮)。 现在我面临的问题是,当我在列表视图中添加新的测验时,倒计时限制为30秒,此新测验的倒计时在30秒后停止,但第一个测验的倒计时也显示在第二个测验的开始按钮
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
// Check if the existing view is being reused, otherwise inflate the view
View listItemView = convertView;
if(listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.upcoming_quiz, parent, false);
}
// Get the {@link QuizCard} object located at this position in the list
QuizCard quizCard = getItem(position);
// Find the start quiz button
final Button start_quiz_btn = (Button) listItemView.findViewById(R.id.start_quiz_btn);
// Set countDownTimer
CountDownTimer countDownTimer = new CountDownTimer(quizCard.getCountDownTime(), 1000) {
@Override
public void onTick(long millisUntilFinished) {
start_quiz_btn.setText(Long.toString(millisUntilFinished / 1000));
}
@Override
public void onFinish() {
// Set the button clickable
start_quiz_btn.setClickable(true);
// Set the button enabled
start_quiz_btn.setEnabled(true);
// Change the text of the button
start_quiz_btn.setText(R.string.start_quiz);
}
};
// Start the count down timer
countDownTimer.start();
return listItemView;
}
在我的代码中,我正在做的是创建了一个自定义的listview adpater,在其中将倒数计数上限传递给何时开始测验。