我以前搜索过很多,但我没有达到我的目标
我的问题是
我有一个带有计时器的列表视图,可以在每一行中倒计时,当我单击一行时,计数器会立即倒计时,但是当我单击另一行时,前一个计数器停止并且当前计数器倒计时,但它会将第二个计数为两个秒。
所以我的问题是:-
我想让与单击的行相关的计数器运行。不停止任何以前运行的计数器。
对不起我的英语不好,提前感谢。
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class RecipeStepsActivity extends Activity {
ListView list;
int minutes;
int seconds;
int hours;
int time;
TextView fullTime;
TextView step;
String fullTimeString;
String[] fullTimeArray;
MediaPlayer myTen ;
Activity context;
@SuppressLint("ShowToast")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recipe_steps);
context=this;
list = (ListView) findViewById(R.id.listId);
list.setAdapter(new RecipeStepsListAdapter(RecipeStepsActivity.this,R.layout.activity_recipe_steps_list_adapter,XmlHandler.HashMapOfSteps.get(Main.position),XmlHandler.HashMapOfTime.get(Main.position)));
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v, int id,
long arg3) {
// TODO Auto-generated method stub
final String item = (String) parent.getItemAtPosition(id);
fullTimeArray = new String[3];
fullTimeArray = item.split(":");
Toast.makeText(RecipeStepsActivity.this,
"h "+fullTimeArray[0]+":m "+fullTimeArray[1]+":s "+fullTimeArray[2], Toast.LENGTH_SHORT).show();
step = (TextView)findViewById(R.id.step);
fullTime = (TextView)findViewById(id);
hours = Integer.parseInt(fullTimeArray[0]);
minutes = Integer.parseInt(fullTimeArray[1]);
seconds = Integer.parseInt(fullTimeArray[2]);
time = (hours*60*60)+(minutes*60)+seconds;
createCounterDownTimer();
}
});
}
public void createCounterDownTimer(){
runOnUiThread( new Runnable() {
public void run() {
// TODO Auto-generated method stub
new CountDownTimer(time * 1000, 1000) {
@SuppressLint("NewApi")
@Override
public void onTick(long millisUntilFinished) {
if(hours>0 && minutes == 0 && seconds == 0){
hours -- ;
fullTime.setText(hours+":"+minutes+":"+seconds);
fullTime.animate();
minutes = 60;
}
if( minutes>0 && seconds == 0){
minutes--;
fullTime.setText(hours+":"+minutes+":"+seconds);
fullTime.animate();
seconds = 60;
//seconds --;
}
if(seconds>0 ){
seconds--;
fullTime.setText(hours+":"+minutes+":"+seconds);
fullTime.animate();
}
}
@Override
public void onFinish() {
// TODO Auto-generated method stub
myTen = MediaPlayer.create(RecipeStepsActivity.this, R.raw.soundd);
myTen.start();
//this is a run for the ui ajax containing alertdialog
runOnUiThread( new Runnable() {
@SuppressWarnings("deprecation")
public void run() {
fullTime.setText("time up!");
//showNotification();
AlertDialog alertDialog = new AlertDialog.Builder(RecipeStepsActivity.this).create();
alertDialog.setTitle("time up");
alertDialog.setMessage(step.getText().toString()+" is finished");
alertDialog.setButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
myTen.stop();
dialog.cancel();
}
});
alertDialog.show();
}
});
}
}.start();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.recipe_steps, menu);
return true;
}
}