gpt4 book ai didi

android - 带有 countDownTimer 的 onFinish 方法问题的 listView

转载 作者:行者123 更新时间:2023-11-30 03:15:18 24 4
gpt4 key购买 nike

我对“countDownTimer”有一个问题,当我在 ListView 中的两个不同行中运行两个计数器时,“onfinish”函数仅对最后单击的行进行..

因此,“onfinish”方法使对话框显示最后一个 TextView 的文本。

但是,我希望“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;
}
}

最佳答案

好的,根据您的要求执行此操作,更简单和干净。

  1. 创建处理程序。
  2. 单击行后,放置一条延迟消息,其中包含您希望将文本从单击行的 textView 附加到处理程序的时间间隔,这将在提供的时间间隔结束后由处理程序执行。
  3. 如果您想取消任何消息,只需从处理程序调用 removeCallback 以获取您想要撤消的特定回调。

  4. 在处理程序的 handleMessage block 中编写对话框弹出代码,在那里您可以弹出一个带有所需消息的警告框。

希望对您有所帮助。

关于android - 带有 countDownTimer 的 onFinish 方法问题的 listView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20242251/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com