2013-11-27 58 views
1

我有一個「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; 
} 
} 
+0

希望每個單擊某一行的時間?你你需要它只有一次的第一單擊項目被彈出被彈出的對話框? – Techfist

+0

不,有一個計數器countDown,當我點擊原始...計數器達到值(0:0:0)後,對話框出現包含一個textview中存在的文本行.. 我需要當它們的計數器達到(0) –

+0

好,請檢查我的答案 – Techfist

回答

0

好了,根據您的需要做到這一點,更簡單和乾淨。

  1. 創建一個處理程序。
  2. 單擊某一行後,拖放一個延遲的消息,並將時間間隔設置爲您要將單擊行的textView的文本附加到處理程序,這將在提供的時間間隔過後由處理程序執行。
  3. 如果您想要取消任何消息,只需調用removeCallback處理函數來調用要撤銷的特定回調函數。

  4. 在處理程序的handleMessage塊內寫入對話框彈出代碼,您可以彈出一個包含所需消息的警告框。

希望它有幫助。

+0

感謝您的幫助,但是如果有代碼示例關於該代碼示例,它將被很好理解:) 我已經解決了問題通過intick()方法中的一個條件,檢查它是否不介意並給我反饋 –

1

我通過把一個條件在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; 
} 
}