如何在listview中的每一行上创建一个线程

时间:2013-11-13 12:50:25

标签: android multithreading listview

我之前搜索了很多,但我没有达到我的目标

我的问题是

我有一个带有计时器的列表视图,可以在每行中倒计时 当我点击一行时,计数器立即倒计时 ,但是当我点击另一行时,前一个计数器停止,当前计数器倒计时,但它将秒计数为两秒。

所以我的问题是: -

我想让计数器运行与点击的行相关。不停止任何先前运行的计数器。

抱歉我的英语不好,并提前致谢。

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

2 个答案:

答案 0 :(得分:1)

我已经解决了在2秒内计算秒数的问题,以及停止前一个计数器的问题, 通过创建一个名为“createCounterDownTimer”的方法,我将listview中使用的所有textview放在此方法中,并使用当前单击行的“id”,并使用“CountDownTimer(以毫秒为单位的时间,间隔)”...

这是活动的代码

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

}

答案 1 :(得分:0)

使用AsyncTask概念。通过使用它,您可以运行多个线程,而不会打扰UI线程。只是谷歌它你会得到很多关于AsyncTask概念的教程。