我为此应用程序设置了一个计时器,因此,当计时器结束并且仍选中Itemdata中的框时,它应调用Thank.class活动。
但是,我意识到它多次对Thanks.class活动进行了分类。
例如。如果有3个选定的项目,它将调用Thank.class活动3次。
这是我宣布的计时器
//Declare timer
CountDownTimer cTimer = null;
//start timer function
void startTimer() {
cTimer = new CountDownTimer(10000, 1000) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
Intent intent = new Intent(ctx, Thanks.class);
ctx.startActivity(intent);
}
};
cTimer.start();
}
//cancel timer
void cancelTimer() {
if(cTimer!=null)
cTimer.cancel();
}
这是我如何调用该函数。
@Override
public View getView(final int position, final View view, ViewGroup parent) {
final GridItemData itemData = getItem(position);
inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View itemView = inflater.inflate(R.layout.grid_image, parent, false);
ivGallery = itemView.findViewById(R.id.grid_item_image);
ivGallery.setImageResource(getItem(position).imageUrl);
final MainActivity main = new MainActivity();
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ivGallery = itemView.findViewById(R.id.grid_item_image);
itemData.setSelected(!itemData.isSelected());
startTimer();
if(itemData == getItem(8)) {
AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
customizeAlert(builder, v);
builder.show();
}
else if (itemData == getItem(0)) {
ivGallery.setImageResource(itemData.isSelected()?R.drawable.clickfloor: R.drawable.floor);
}
else if (itemData == getItem(1)) {
ivGallery.setImageResource(itemData.isSelected()?R.drawable.cb: R.drawable.wb);
}
else if (itemData == getItem(2)) {
ivGallery.setImageResource(itemData.isSelected()?R.drawable.cs: R.drawable.s);
}
else if (itemData == getItem(3)) {
ivGallery.setImageResource(itemData.isSelected()?R.drawable.clickothers: R.drawable.others);
}
}
});
return itemView;
}
请告诉我该怎么办,谢谢!
最佳答案
只启动一次CountDownTimer
怎么样?
void startTimer() {
if(cTimer == null) {
cTimer = new CountDownTimer(10000, 1000) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
Intent intent = new Intent(ctx, Thanks.class);
ctx.startActivity(intent);
}
};
cTimer.start();
}
}
或者,当第二个
CountDownTimer
将要启动时,只需停止第一个。