Android的:在ListView多个同时发生的倒计时定时器 [英] Android: Multiple simultaneous count-down timers in a ListView

查看:1004
本文介绍了Android的:在ListView多个同时发生的倒计时定时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个应用程序,需要与一个未确定的数量的元件,其每一个具有从一个可变数目的递减计数计时器一个ListView。我能够成功地使一个他们的倒计时,但我无法弄清楚如何将在ListView中的每个元素的计时器。

我目前使用的是 CountDownTimer (一定要大写d。如果从复制网站,他们有它错了)。

任何code或来源点我在正确的方向有多少AP preciated。

下面是我目前的EventAdapter类,它设置在每个ListView控件元素的TextView中显示的文本。我需要做的就是让TextView的倒计时每一秒。由于ListView中的每个元素显示不同的东西,我想我需要区分每个元素的一种方式。

我可以只更新整个列表每一秒,但也有我没有包含其它元素如从因特网加载的图像,这将是不现实的刷新每一秒。

 私有类EventAdapter扩展ArrayAdapter<事件>
{
    私人的ArrayList<事件>项目;

    公共EventAdapter(上下文的背景下,INT textViewResourceId,ArrayList的<事件>项目){
        超(背景下,textViewResourceId,项目);
        this.items =项目;
    }

    @覆盖
    公共查看getView(INT位置,查看convertView,ViewGroup中父){
        视图V = convertView;
        如果(V == NULL){
            LayoutInflater VI =(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            V = vi.inflate(R.layout.row,NULL);
        }

        事件E = items.get(位置);

        如果(E!= NULL){
            TextView的电视=(TextView中)v.findViewById(R.id.text);

            如果(电视!= NULL)
                tv.setText(e.getName());
        }
        返回伏;
    }
}
 

解决方案

请看看这里在我的博客在那里你会找到如何实现这一目标的一个例子。

一个解决方案是把该重新presents每个柜台到一个HashMap连同它在列表中的关键地位的TextView。

在getView()

  TextView的计数器=(TextView中)v.findViewById(R.id.myTextViewTwo);
如果(计数!= NULL){
    counter.setText(myData.getCountAsString());
    //添加的TextView的专柜HashMap中。
    mCounterList.put(位置,柜);
}
 

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

 私人最终可运行mRunnable =新的Runnable(){
    公共无效的run(){
        迈德特myData的;
        的TextView TextView的;

        //如果计数器是活动
        如果(mCountersActive){
            如果(mCounterList = NULL和放大器;!&安培;!mDataList = NULL){
                的for(int i = 0; I< mDataList.size();我++){
                    myData的= mDataList.get(ⅰ);
                    的TextView = mCounterList.get(ⅰ);
                    如果(TextView的!= NULL){
                        如果(myData.getCount()> = 0){
                            textView.setText(myData.getCountAsString());
                            myData.reduceCount();
                        }
                    }
                }
            }
            //更新每一秒
            mHandler.postDelayed(这一点,1000);
        }
    }
};
 

I am creating an app that requires a ListView with an undetermined number of elements, each of which has a timer that counts down from a variable number. I am able to successfully make one of them count down, but I can't figure out how to include a timer in each element of the ListView.

I am currently using a CountDownTimer (make sure to capitalize the D if copying from the website, they have it wrong).

Any code or sources to point me in the right direction are much appreciated.

Here is my current EventAdapter class, it sets the text displayed in each ListView element's TextView. What I need to do is make the TextView count down every second. Since each element of the ListView is displaying something different, I suppose I need a way of differentiating each element.

I could just update the whole list every second, but there are other elements I have not included such as images loaded from the internet that it would be impractical to refresh every second.

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

解决方案

Please have a look here at my blog where you will find an example on how to achieve this.

One solution is to put the TextView that represents each counter into a HashMap together with it's position in the list as the key.

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

Then you can update the counters by using a Handler and where you post a runnable.

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

这篇关于Android的:在ListView多个同时发生的倒计时定时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆