Android Development-----02-listview Some entries in the countdown

Source: Internet
Author: User

This article focuses on using the countdown in the ListView

in the development of Android, we often meet the countdown operation, usually using the timer and handler common operation to complete. Of course you can also use the Android system control Countdowntimer, here is a simple operation of this control:

New Countdowntimer (30000, +) {public     void OnTick (Long millisuntilfinished) {     timetv.settext (":" + millisuntilfinished/1000);     }     public void OnFinish () {     timetv.settext ("done!");}     }. Start ();

This operation is better than a timer.

Of course, this article is not to show this simple effect, in the project encountered such a problem, add a countdown in the ListView, some entries have a countdown function, some items do not have the function of countdown, this way how to achieve it? In adapter, each entry is added as the code above. As shown below:

Public View GetView (int position, View Convertview, ViewGroup parent) {Goods Goods = goodss.get (position); if (Convertview = = null) {Convertview = View.inflate (listviewcounttimedemo.this, r.layout.item_counttime, NULL); TextView Nametv = (TextView) Convertview.findviewbyid (R.ID.NAMETV); final TextView Timetv = (TextView) Convertview.findviewbyid (R.id.timetv); Nametv.settext (Goods.getname ()); Timetv.settext (Goods.getTime () + ""); new Countdowntimer (30000, +) {public     void OnTick (Long millisuntilfinished) {     timetv.settext (":" + millisuntilfinished/1000);     }     public void OnFinish () {     timetv.settext ("done!");}     }. Start (); }else{}return Convertview;}

There must be a problem with this approach, and each time a new entry is taken, a timing object is created, so that when the entry is more, it goes back and forth, and then a timing disorder occurs. It's not about 1 seconds and 1 seconds. It is possible to create too many Countdowntimer objects, resulting in excessive memory consumption.

A personal recommendation is to use the following method:

Package Com.example.androidtest;import Java.util.arraylist;import Android.app.activity;import android.os.Bundle; Import Android.os.countdowntimer;import android.os.handler;import android.view.view;import android.view.ViewGroup; Import Android.widget.baseadapter;import Android.widget.listview;import Android.widget.textview;public class Countdownactivity extends Activity {private ListView lv;private arraylist<goods> Goodss = new arraylist<goods& gt; (); @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_countdown); LV = (ListView) Findviewbyid (r.id.lv); for (int i=0;i<20;i++) { Goodss.add (New Goods (30000+i*1000, "Zhangfei" +i)); adapter = new Counttimeadapter (); Lv.setadapter (adapter); Handler.sendemptymessage (1);} Private class Counttimeadapter extends baseadapter{@Overridepublic int GetCount () {return goodss.size ();} @Overridepublic Object getItem (int position) {return goodss.get (position);} @Overridepublic LongGetitemid (int position) {return position;} @Overridepublic view GetView (int position, view Convertview, ViewGroup parent) {Goods Goods = goodss.get (position); if (con Vertview = = null) {Convertview = View.inflate (countdownactivity.this, r.layout.item_counttime, null);} TextView Nametv = (TextView) Convertview.findviewbyid (R.ID.NAMETV); final TextView Timetv = (TextView) Convertview.findviewbyid (R.id.timetv); Nametv.settext (Goods.getname ()); Long seconds = Goods.gettime ()/1000; Timetv.settext (seconds/(60) + "min" +seconds%60+ "second"); return Convertview;}} Private Handler Handler = new Handler () {public void Handlemessage (android.os.Message msg) {switch (msg.what) {case 1:boole An isneedcounttime = False;//①: Actually in this block need to calculate the current time accurately for (int index =0;index<goodss.size (); index++) {Goods Goods = Goodss.get (index); Long time = Goods.gettime (); if (time>1000) {//Determine if there are any entries to count down, and if you can countdown, delay one second, Let it then countdown isneedcounttime = True;goods.settime (time-1000);} Else{goods.settime (0);}} ②:for Cycle Execution Time adapter.notifydatasetchanged (); <span Style= "COLOR: #009900;" >if (isneedcounttime) {//todo then uses 1000-(②-①) to win the delay time handler.sendemptymessagedelayed (1, 1000);} Break;</span>}}};p rivate counttimeadapter adapter;}
The focus of this article is to use the handler delay operation to perform the countdown operation. So the countdown is complete, swipe up and down will not be the problem of disorder.

Picture effect:





Android Development-----02-listview Some entries in the countdown

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.