1

I've searched alot before but I didn't reach my target

My problem is that

I have a listview with a timer to count down in each row and when I click on one row the counter counts down immediately ,but when I click on another row the previous counter stops and the current one counts down but it counts the second as two seconds .

so my problem is :-

I want to make the counters run which it is relevant to the clicked row . without stopping any previous running counters.

sorry for my bad English and thanks in advance.

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;
    int time;
    TextView fullTime;
    TextView step;
    String fullTimeString;
    String[] fullTimeArray;
    MediaPlayer myTen ;
    Activity context;
    @SuppressLint("ShowToast")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recipe_steps);
        context=this;

        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 id,
                    long arg3) {
                // TODO Auto-generated method stub
                final String item = (String) parent.getItemAtPosition(id);
                fullTimeArray = new String[3];
                fullTimeArray = item.split(":");
                Toast.makeText(RecipeStepsActivity.this,
                        "h "+fullTimeArray[0]+":m "+fullTimeArray[1]+":s "+fullTimeArray[2], Toast.LENGTH_SHORT).show();

                step = (TextView)findViewById(R.id.step);
                fullTime = (TextView)findViewById(id);

                hours = Integer.parseInt(fullTimeArray[0]);
                minutes = Integer.parseInt(fullTimeArray[1]);
                seconds = Integer.parseInt(fullTimeArray[2]);
                time = (hours*60*60)+(minutes*60)+seconds;
                createCounterDownTimer();

            }
        });
    }




    public void createCounterDownTimer(){
        runOnUiThread( new Runnable() {
            public void run() {
                // TODO Auto-generated method stub


        new CountDownTimer(time * 1000, 1000) {

            @SuppressLint("NewApi")
            @Override
            public void onTick(long millisUntilFinished) {


                if(hours>0 && minutes == 0 && seconds == 0){
                    hours -- ;

                    fullTime.setText(hours+":"+minutes+":"+seconds);
                    fullTime.animate();
                    minutes = 60;
                }
                if( minutes>0 && seconds == 0){
                    minutes--;
                    fullTime.setText(hours+":"+minutes+":"+seconds);
                    fullTime.animate();
                    seconds = 60;
                    //seconds --;
                }
                if(seconds>0 ){
                    seconds--;
                    fullTime.setText(hours+":"+minutes+":"+seconds);
                    fullTime.animate();

                }

            }

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

            myTen = MediaPlayer.create(RecipeStepsActivity.this, R.raw.soundd);
            myTen.start();
            //this is a run for the ui ajax containing alertdialog
            runOnUiThread( new Runnable() {
                @SuppressWarnings("deprecation")
                public void run() {
                    fullTime.setText("time up!");
                    //showNotification();
                    AlertDialog alertDialog = new AlertDialog.Builder(RecipeStepsActivity.this).create();
                    alertDialog.setTitle("time up");
                    alertDialog.setMessage(step.getText().toString()+" is finished");
                    alertDialog.setButton("ok", new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // TODO Auto-generated method stub
                            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;
    }
}
Nouran Mahmoud
  • 309
  • 1
  • 2
  • 13

2 Answers2

0

Use AsyncTask concept. By using that you can run multiple threads without disturbing UI thread. Just google it you ll get lot of tutorials on AsyncTask concept.

saa
  • 1,538
  • 2
  • 17
  • 35
  • 1
    This is not applicable here, as there can only one AsyncTask run at a time. If you create and execute several, they wait until the previous task has stopped. Strange but I ran into that and looked it up. – Ridcully Nov 13 '13 at 13:04
  • In android we can run multiple AsyncTask(s) parallel y. Please refer this doc for more details i.e Order of execution http://developer.android.com/reference/android/os/AsyncTask.html – saa Nov 13 '13 at 13:07
  • Actually it is not. You can execute several in a row, but they are enqueued and handled one after another. See http://stackoverflow.com/questions/4068984/running-multiple-asynctasks-at-the-same-time-not-possible – Ridcully Nov 14 '13 at 06:32
  • I've tried AsyncTask before but it didn't fit with my problem. – Nouran Mahmoud Nov 16 '13 at 10:36
0

I've solved the problem of counting the second in 2 seconds ,and the problem of stopping the previous counters, by making a method called "createCounterDownTimer" and I put all the textviews used in the listview in this method and by using the "id" of the current clicked row ,and by using "CountDownTimer(time in millisecond,interval)" ...

and this's the code of the activity

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;

@SuppressLint("ShowToast")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_recipe_steps);
    context = this;

    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 id,long arg3) {
        parent1=parent;
        final String item = (String) parent1.getItemAtPosition(id);
        fullTimeArray = new String[3];
        fullTimeArray = item.split(":");
        Toast.makeText(RecipeStepsActivity.this,
                "h " + fullTimeArray[0] + ":m " + fullTimeArray[1]
                        + ":s " + fullTimeArray[2], Toast.LENGTH_SHORT).show();
            hours = Integer.parseInt(fullTimeArray[0]);
            minutes = Integer.parseInt(fullTimeArray[1]);
            seconds = Integer.parseInt(fullTimeArray[2]);
            Long times = ((hours * 60L * 60L) + (minutes * 60L) + seconds) * 1000;
            createCounterDownTimer(times,id);

        }
    });

}

public void createCounterDownTimer(final Long times,final int id) {
    runOnUiThread(new Runnable() {
        public void run() {

            new CountDownTimer(times, 1000) {

                @SuppressLint("NewApi")
                @Override
                public void onTick(long ms) {
                    step = (TextView) findViewById(R.id.step);
                    fullTime = (TextView) findViewById(id);

                    int seconds = (int) (ms / 1000) % 60;
                    int minutes = (int) ((ms / (1000 * 60)) % 60);
                    int hours = (int) ((ms / (1000 * 60 * 60)) % 24);
                    System.out.println(hours + ": " + minutes + ": "
                            + seconds);
                    fullTime.setText(hours + ": " + minutes + ": "
                            + seconds);
                }

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

                    myTen = MediaPlayer.create(RecipeStepsActivity.this,
                            R.raw.soundd);
                    myTen.start();
                    // this is a run for the ui ajax containing alertdialog

                    fullTime.setText("time up!");
                    // showNotification();
                    AlertDialog alertDialog = new AlertDialog.Builder(
                            RecipeStepsActivity.this).create();
                    alertDialog.setTitle("time up");
                    alertDialog.setMessage(step.getText()
                            .toString() + " is finished");
                    alertDialog.setButton("ok",
                            new DialogInterface.OnClickListener() {

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

}

Nouran Mahmoud
  • 309
  • 1
  • 2
  • 13