1

I have created a listview with count down timer, the result shows good in first time but when scrolls the list it gets shuffled and result gets wrong.

Adapter class:

public class EventsListAdapter extends BaseAdapter {

    LayoutInflater inflater;
    Activity context;

    List<MyEvents> list;

    long startTime = 0L;
    long countUp;
    String hrs = "00";
    String mins = "00";
    String secs = "00";
    private MyCustomTimer myTimer;
    public EventsListAdapter(Activity context, List<MyEvents> list) {
        this.context = context;
        inflater = context.getLayoutInflater();
        this.list = list;
    }

    public void setActivityList(List<MyEvents> LoadList) {
        list = LoadList;
    }

    public int getCount() {
        return list.size();
    }

    public Object getItem(int position) {
        return list.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    static class ViewHolder {
        TextView name;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        MyEvents eventsData = list.get(position);   

        View rowView = convertView;
        ViewHolder viewHolder;
        if (rowView == null) {
            LayoutInflater inflater = context.getLayoutInflater();
            rowView = inflater.inflate(R.layout.likes_inflate, null);
            viewHolder = new ViewHolder();
            viewHolder.name = (TextView) rowView.findViewById(R.id.lblName);
            if (eventsData.isStarted()){
                myTimer = new MyCustomTimer(viewHolder.name);
                myTimer.setTimer(eventsData.start_date);
            }
            rowView.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder) rowView.getTag();
        }

        return rowView;
    }
}

CountDownTimer class:

class MyCustomTimer{
        TextView tv;
        public MyCustomTimer(TextView tv) {
            this.tv = tv;
        }

        void setTimer(final long time) {
            new CountDownTimer(time, 1000) {
                public void onTick(long millisUntilFinished) {
                    //Set formatted date to your TextView                   
                    tv.setText(Utilities.getDurationBreakdown(millisUntilFinished));

                }
                public void onFinish() {
                    tv.setText("Done!");
                }
            }.start();
        }
    }

Is anythig wrong with this code? Please suggest.

fargath
  • 7,844
  • 6
  • 24
  • 36

2 Answers2

0

When ever you scroll the list view, getView() is called. So the counter starts again. Maintain the counter in a variable like hash map. check this variable in getView() and then set the value of counter according to it.

Sagar Patil
  • 1,400
  • 15
  • 29
  • I have checked the counter already started or not, if it started i do not call the timer. I have edited the code. Still the same problem. – fargath Jul 31 '13 at 14:22
  • 2
    If you find the counter is started, set the counter again to the time where it left. That is, take a variable and assign counter time to it. If counter is 10 the variable should be 10. So in getView() assign counter with that variable. hope you get this. – Sagar Patil Aug 01 '13 at 10:05
  • I got the solution, i assigned a variable to set the counter and make the adapter notifydatasetchanged() for every tick on timer. now it works great. – fargath Aug 02 '13 at 06:34
  • after calling notifydatasetchanged() on every tick, list gets slow when scrolling. :( – fargath Aug 12 '13 at 12:18
  • @fargath : hi friend, can you please give me a demo, because i also wanted to display countdown timer for each listrow in listview. – Ajay Dec 19 '13 at 06:31
  • any answer with sample? – Arthur Melo Jun 14 '16 at 07:04
  • @ArthurMelo you can now try with Recycler view which is replacement for list view – Sagar Patil Jun 14 '16 at 07:14
-1

You can check my answer here https://stackoverflow.com/a/53543180/6711554. I have used RecyclerView but you can do same for ListView.