Kevin Kevin - 1 month ago 17
Android Question

Android: Multiple simultaneous count-down timers in a ListView

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

Answer

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