Search code examples
javaandroidarrayscountdown

when is countdown start push data into array java


I am using frequency detector and when frequency is high i want start countdown with 10 $ and collect high frequency into a data array.

  if(maxAmpFreq > 3000){

  Log.i("tag","start.......");
  new CountDownTimer(10000, 1000) {
    String test = String.valueOf(maxAmpFreq);
    ArrayList<String> mylist = new ArrayList<String>();


    public void onTick(long millisUntilFinished) {
      Log.i("tag","A Kiss after 5 seconds");

      mylist.add(test);

    }

    public void onFinish() {
      Log.i("tag","your test is......"+mylist);
    }
  }.start();

}

but this not working correctly please guild me to solve this.


Solution

  • Problem is You declare your ArrayList Locally , That's why it have only one value at last. Every time its create new ArrayList

    Declare globally

    private    ArrayList<String> mylist = new ArrayList<String>();
    

    and use in the method for adding the value

    mylist.add(test);
    

    NOTE:- Try to declare variable globally, which is used in whole class methods