我有一個「countDownTimer」的問題,當我在列表視圖中的兩個不同的行中運行兩個計數器時,「onfinish」功能只進行最後單擊行..listView與countDownTimer的onFinish方法問題
所以,「onfinish」方法會使對話框顯示上次點擊的文本視圖的文本。
但是,我希望「onfinish」方法顯示完成計數的特定點擊行的對話框。
這是代碼
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;
TextView fullTime;
TextView step;
String fullTimeString;
String[] fullTimeArray;
MediaPlayer myTen;
Activity context;
long init = 0, now;
AdapterView<?> parent1;
CountDownTimer x;
static boolean[] arr;
@SuppressLint("ShowToast")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recipe_steps);
context = this;
arr = new boolean[XmlHandler.HashMapOfTime.size()];
for (int i = 0; i < arr.length; i++) {
arr[i] = false;
}
System.out.println(XmlHandler.HashMapOfTime.size()
+ "++++++++++++++++++++++");
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 newid,
long arg3) {
parent1 = parent;
final String item = (String) parent1.getItemAtPosition(newid);
fullTimeArray = new String[3];
fullTimeArray = item.split(":");
hours = Integer.parseInt(fullTimeArray[0]);
minutes = Integer.parseInt(fullTimeArray[1]);
seconds = Integer.parseInt(fullTimeArray[2]);
Long times = ((hours * 60L * 60L) + (minutes * 60L) + seconds) * 1000;
for (int i = 0; i < arr.length; i++) {
if (i == newid && arr[newid] == false) {
Toast.makeText(RecipeStepsActivity.this, "Counter begin",
Toast.LENGTH_SHORT).show();
createCounterDownTimer(times, newid);
arr[newid] = true;
break;
} else if (i == newid && arr[newid] == true) {
Toast.makeText(RecipeStepsActivity.this,
"Counter stopped", Toast.LENGTH_SHORT).show();
arr[newid] = false;
x.cancel();
break;
}
}
}
});
}
public void createCounterDownTimer(final Long times, final int id) {
runOnUiThread(new Runnable() {
public void run() {
x = new CountDownTimer(times, 1000) {
@SuppressLint("NewApi")
@Override
public void onTick(long ms) {
// System.out.println("===");
fullTime = (TextView) findViewById(id);
step = (TextView) findViewById(id + 10);
System.out.println(step.getId()+" of the onTick step");
// step.setId(id);
System.out.println(step.getId()+" -------------------------"+step.getText()+"---------------------- ");
System.out.println(fullTime.getId()+" -------------------------"+fullTime.getText()+"---------------------- ");
int seconds = (int) (ms/1000) % 60;
int minutes = (int) ((ms/(1000 * 60)) % 60);
int hours = (int) ((ms/(1000 * 60 * 60)) % 24);
System.out.println(id+" ===> "+hours + ": " + minutes +
": "
+ seconds);
fullTime.setText(hours + ": " + minutes + ": "
+ seconds);
}
@SuppressWarnings("deprecation")
@Override
public void onFinish() {
// TODO Auto-generated method stub
fullTime.setText("time up!");
System.out.println(step.getId()+" finished id step");
System.out.println(fullTime.getId()
+ " =============== " + fullTime.getText()
+ "===" + step.getText());
myTen = MediaPlayer.create(RecipeStepsActivity.this,
R.raw.soundd);
myTen.start();
AlertDialog alertDialog = new AlertDialog.Builder(
RecipeStepsActivity.this).create();
alertDialog.setTitle("time up");
alertDialog.setMessage(fullTime.getText().toString()
+ " is finished");
alertDialog.setButton("ok",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
// TODO Auto-generated method
// stub
System.out.println(step.getId()+" onclick dialog finished id step");
System.out.println("finished");
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;
}
}
希望每個單擊某一行的時間?你你需要它只有一次的第一單擊項目被彈出被彈出的對話框? – Techfist
不,有一個計數器countDown,當我點擊原始...計數器達到值(0:0:0)後,對話框出現包含一個textview中存在的文本行.. 我需要當它們的計數器達到(0) –
好,請檢查我的答案 – Techfist