Android:ListView中的多个同时倒计时器

时间:2011-06-14 15:28:57

标签: java android listview countdown

我正在创建一个应用程序,它需要一个具有未确定数量元素的ListView,每个元素都有一个从变量号倒计时的计时器。我能够成功地使一个倒计时,但我无法弄清楚如何在ListView的每个元素中包含一个计时器。

我目前正在使用CountDownTimer(如果从网站上复制,请确保将D大写,但是他们错了)。

非常感谢任何指引我正确方向的代码或来源。

这是我当前的EventAdapter类,它设置每个ListView元素的TextView中显示的文本。我需要做的是每秒使TextView倒数。由于ListView的每个元素都显示不同的东西,我想我需要一种区分每个元素的方法。

我可以每秒更新整个列表,但还有其他一些我没有包含的元素,例如从互联网上加载的图像,每秒刷新一次是不切实际的。

private class EventAdapter extends ArrayAdapter<Event>
{
    private ArrayList<Event> items;

    public EventAdapter(Context context, int textViewResourceId, ArrayList<Event> items) {
        super(context, textViewResourceId, items);
        this.items = items;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.row, null);
        }

        Event e = items.get(position);

        if (e != null) {
            TextView tv = (TextView) v.findViewById(R.id.text);

            if (tv != null)
                tv.setText(e.getName());
        }
        return v;
    }
}

4 个答案:

答案 0 :(得分:11)

这是我这样做的一个例子,它完美无缺:

public class TestCounterActivity extends ListActivity
{
    TestAdapter adapter;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        // Example values
        ArrayList<Date> values = new ArrayList<Date>();
        values.add(new Date(1482464366239L));
        values.add(new Date(1480464366239L));
        values.add(new Date(1470464366239L));
        values.add(new Date(1460464366239L));
        values.add(new Date(1450464366239L));
        values.add(new Date(1440464366239L));
        values.add(new Date(1430464366239L));
        values.add(new Date(1420464366239L));
        values.add(new Date(1410464366239L));
        values.add(new Date(1490464366239L));

        adapter = new TestAdapter(this, values);

        setListAdapter(adapter);
    }

    @Override
    protected void onStop()
    {
        super.onStop();

        // Dont forget to cancel the running timers
        adapter.cancelAllTimers();
    }
}

这是适配器

public class TestAdapter extends ArrayAdapter<Date> 
{
    private final Activity context;
    private final List<Date> values;
    private HashMap<TextView,CountDownTimer> counters;

    static class TestViewHolder 
    {
        public TextView tvCounter;
    }

    public TestAdapter(Activity context, List<Date> values) 
    {
        super(context, R.layout.test_row, values);
        this.context = context;
        this.values = values;
        this.counters = new HashMap<TextView, CountDownTimer>();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) 
    {
        View rowView = convertView;

        if(rowView == null)
        {
            LayoutInflater inflater = context.getLayoutInflater();
            rowView = inflater.inflate(R.layout.test_row, null);
            final TestViewHolder viewHolder = new TestViewHolder();
            viewHolder.tvCounter = (TextView) rowView.findViewById(R.id.tvCounter);

            rowView.setTag(viewHolder);
        }

        TestViewHolder holder = (TestViewHolder) rowView.getTag();
        final TextView tv = holder.tvCounter;

        CountDownTimer cdt = counters.get(holder.tvCounter);
        if(cdt!=null)
        {
            cdt.cancel();
            cdt=null;
        }

        Date date = values.get(position);
        long currentDate = Calendar.getInstance().getTime().getTime();
        long limitDate = date.getTime();
        long difference = limitDate - currentDate;

        cdt = new CountDownTimer(difference, 1000)
        {
            @Override
            public void onTick(long millisUntilFinished) 
            {
                int days = 0;
                int hours = 0;
                int minutes = 0;
                int seconds = 0;
                String sDate = "";

                if(millisUntilFinished > DateUtils.DAY_IN_MILLIS)
                {
                    days = (int) (millisUntilFinished / DateUtils.DAY_IN_MILLIS);
                    sDate += days+"d";
                }

                millisUntilFinished -= (days*DateUtils.DAY_IN_MILLIS);

                if(millisUntilFinished > DateUtils.HOUR_IN_MILLIS)
                {
                    hours = (int) (millisUntilFinished / DateUtils.HOUR_IN_MILLIS);
                }

                millisUntilFinished -= (hours*DateUtils.HOUR_IN_MILLIS);

                if(millisUntilFinished > DateUtils.MINUTE_IN_MILLIS)
                {
                    minutes = (int) (millisUntilFinished / DateUtils.MINUTE_IN_MILLIS);
                }

                millisUntilFinished -= (minutes*DateUtils.MINUTE_IN_MILLIS);

                if(millisUntilFinished > DateUtils.SECOND_IN_MILLIS)
                {
                    seconds = (int) (millisUntilFinished / DateUtils.SECOND_IN_MILLIS);
                }

                sDate += " "+String.format("%02d",hours)+":"+String.format("%02d",minutes)+":"+String.format("%02d",seconds);
                tv.setText(sDate.trim());
            }

            @Override
            public void onFinish() {
                tv.setText("Finished");
            }
        };

        counters.put(tv, cdt);
        cdt.start();

        return rowView;
    }

    public void cancelAllTimers()
    {
        Set<Entry<TextView, CountDownTimer>> s = counters.entrySet();
        Iterator it = s.iterator();
        while(it.hasNext())
        {
            try
            {
                Map.Entry pairs = (Map.Entry)it.next();
                CountDownTimer cdt = (CountDownTimer)pairs.getValue();

                cdt.cancel();
                cdt = null;
            }
            catch(Exception e){}
        }

        it=null;
        s=null;
        counters.clear();
    }
}

答案 1 :(得分:7)

请查看here at my blog,您将找到有关如何实现此目标的示例。

一种解决方案是将表示每个计数器的TextView与其在列表中的位置一起放入HashMap中作为键。

在getView()

TextView counter = (TextView) v.findViewById(R.id.myTextViewTwo);
if (counter != null) {
    counter.setText(myData.getCountAsString());
    // add the TextView for the counter to the HashMap.
    mCounterList.put(position, counter);
}

然后,您可以使用处理程序更新计数器以及发布可运行的位置。

private final Runnable mRunnable = new Runnable() {
    public void run() {
        MyData myData;
        TextView textView;

        // if counters are active
        if (mCountersActive) {                
            if (mCounterList != null && mDataList != null) {
                for (int i=0; i < mDataList.size(); i++) {
                    myData = mDataList.get(i);
                    textView = mCounterList.get(i);
                    if (textView != null) {
                        if (myData.getCount() >= 0) {
                            textView.setText(myData.getCountAsString());
                            myData.reduceCount();
                        }
                    }
                }
            }
            // update every second
            mHandler.postDelayed(this, 1000);
        }
    }
};

答案 2 :(得分:4)

在检查了几种方法之后,这是我写的一个创造性的解决方案。简单而且完美。

想要检查更新数据的Runnable是否正在更新相同的TextView,如果TextView与不同的视图相关,Runnable将会停止,通过这种方式,后台将不会有额外的Thread,因此不会出现闪烁的文本或内存泄漏。

1。在getView()内添加每个TextView标记及其位置。

text = (TextView) view
        .findViewById(R.id.dimrix);
text.setTag(position);

2。创建实现Runnable的类,这样我们就可以传递参数。

public class mUpdateClockTask implements Runnable {
    private TextView tv;
    final Handler mClockHandler = new Handler();
    String tag;

    public mUpdateClockTask(TextView tv,
            String tag) {
        this.tv = tv;
        this.tag = tag;
    }

    public void run() {

        if (tv.getTag().toString().equals(tag)) {
                        // do what ever you want to happen every second
            mClockHandler.postDelayed(this, 1000);
        }

    }

};

所以这里发生的事情是TextView不等于Runnable将停止的原始标记。

3。回到getView()

        final Handler mClockHandler = new Handler();
        mUpdateClockTask clockTask = new mUpdateClockTask(text,
                activeDraw, text.getTag().toString());
        mClockHandler.post(clockTask);

就是这样,工作完美!

答案 3 :(得分:1)

这是使用ListView和多个CountDownTimer的另一种解决方案。首先,我们创建一个包含CountDownTimer的类 MyCustomTimer

public class MyCustomTimer{
    public MyCustomTimer() {
    }
    public void setTimer(TextView tv, long time) {
        new CountDownTimer(time, 1000) {
            public void onTick(long millisUntilFinished) {
                //Set formatted date to your TextView
                tv.setText(millisUntilFinished);
            }
            public void onFinish() {
                tv.setText("Done!");
            }
        }.start();
    }
}

然后,在适配器中初始化创建的类:

public class MyAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    private MyCustomTimer myTimer;
    private ArrayList<Item> myItems;

    public MyAdapter(Context context, ArrayList<Item> data) {
        mInflater = LayoutInflater.from(context);
        myTimer= new MyCustomTimer();
        myItems = data;
    }

    //... implementation of other methods

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        convertView = mInflater.inflate(R.layout.listview_row, null);
        TextView tvTimer = (TextView) convertView.findViewById(R.id.textview_timer);
        TextView tvName = (TextView) convertView.findViewById(R.id.textview_name);

        Item item = data.get(position);

        tvName.setText(item.getName());
        myTimer.setTimer(tvTimer, item.getTime());

        return convertView;
    }
}