我对“ countDownTimer”有问题,当我在列表视图的两个不同行中运行两个计数器时,“ 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;
}
}

最佳答案

我已经通过在ontick()方法中添加条件解决了问题

在其中放入一个变量,该变量将保存当前计数的值(步骤文本)。
我已经在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;
    }

    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;
                }
            }

        }
    });

}
String temp ;
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);

                    int seconds = (int) (ms / 1000) % 60;
                    int minutes = (int) ((ms / (1000 * 60)) % 60);
                    int hours = (int) ((ms / (1000 * 60 * 60)) % 24);

                    fullTime.setText(hours + ": " + minutes + ": "
                            + seconds);

                    if(hours == 0 && minutes == 0 && seconds == 1){

                         temp = step.getText().toString();
                         fullTime.setText("time up!");
                    }
                }

                @SuppressWarnings("deprecation")
                @Override
                public void onFinish() {
                    // TODO Auto-generated method stub

                    myTen = MediaPlayer.create(RecipeStepsActivity.this,
                            R.raw.soundd);
                    myTen.start();
                    AlertDialog alertDialog = new AlertDialog.Builder(
                            RecipeStepsActivity.this).create();
                    alertDialog.setTitle("time up");
                    alertDialog.setMessage(temp
                            + " is finished");
                    //arrayyy.remove(0);
                    alertDialog.setButton("ok",
                            new DialogInterface.OnClickListener() {

                                @Override
                                public void onClick(DialogInterface dialog,
                                        int which) {
                                    // TODO Auto-generated method
                                    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;
}
}

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

10-11 22:16