使用Timer更新列表UI

时间:2012-01-23 08:37:59

标签: android timer android-listview handler textview

我正在尝试用timer更新列表视图。我已经实现了android UI计时器教程,但我的问题是如何将它用于列表视图,我需要在一定间隔后更新列表的每一行。处理程序如何更新列表的每一行(即假设textrow位于每行内部,我将显示更新的值。)

公共类ListAdapter扩展了BaseAdapter {

private List<String> messages;
private Context mContext;

public ListAdapter(Context context , List<String> messages){
    this.messages   =  messages;
    mContext   =  context;
}

@Override
public int getCount(){
    return messages.size();
}

@Override
public Object getItem(int location){
    return messages.get(location);
}

@Override
public long getItemId(int index){
    return index;
}

@Override
public View getView(int index , View convertView, ViewGroup viewGroup){

        TextView time = new TextView(mContext);

            //udpate the time.setText() using timer
    convertView = (View)time

    return convertView;
}   

3 个答案:

答案 0 :(得分:3)

创建一个扩展Handler类的customHandler类,并从timer run()方法调用handler的sendEmptyMessage()。

在Handler类中重写handleMessage并更新listview值并调用adapter.notifyDataSetChanged(),它将使用更新的值刷新listview。

以下是示例代码:

Timer timer = new Timer();
CustomTimerTask customTimerTask = new CustomTimerTask();
customTimerTask.run();
timer.scheduleAtFixedRate(customTimerTask, 1000 , 1000);

class CustomTimerTask extends TimerTask {

    @Override
    public void run() {
        myHandler.sendEmptyMessage(0);
    }
}

    class CustomHandler extends Handler{


    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        /// Do your listview update thing
 }

}

答案 1 :(得分:1)

对于使用时钟,我可以基于Handler类提出我自己的类。 它启动/停止计时并在已注册的TextView字段中显示时间和/或日期 对于Timer类,在UI线程中使用它是不安全的(例如,活动和小部件)

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import android.os.Handler;
import android.widget.TextView;
/**
 * The class for creating and refreshing many different fields on different layouts,
 * that can hold actual time and/or date in different formats 
 * The formats should be as in http://developer.android.com/reference/java/text/SimpleDateFormat.html. 
 * Only present and visible fields are being actualized, so there is no need to clean the clock list after closing an activity
 * 
 * Examples of use:
 * 
 *      Clock.registerClock((TextView) findViewById(R.id.TimeField), "HH:mm");
 *      Clock.registerClock((TextView) findViewById(R.id.DateField), "d.M.yyyy EEE");
 *      Clock.start(10000L);
 *
 * @author Petr Gangnus
 */
public final class Clock {
    /**
     * the handler that works instead of timer and supports UI
     */
    static private Handler handler = new Handler();
    /**
     * the interval of the time refreshing
     */
    static private long refreshStep;

    /**
     * pairs TextView timer+time/date format
     */
    private TextView clockFace;
    private String format;
    private Clock(TextView clockFace, String format){
        this.clockFace=clockFace;
        this.format=format;
    }
    // here is the list of views containing the visual timers that should be held actual
    static private ArrayList<Clock> clocks=new ArrayList<Clock>();
    /**
     * fills all timer fields by actual time value, according to their formats.
     */
    static private Runnable mUpdateTimeTask = new Runnable() {
       public void run() {
           for(Clock clock:clocks){
               showActualTimeDate(clock);
           }
           handler.postDelayed(this,refreshStep);
       }
    };

    //============================================ public members ====================================================================
    /**
     * add a clock to the list of updating clocks
     * @param clockFace - the place where the time or date will be shown 
     * @param format - the format of the time/date 
     * @return
     */
    public static boolean registerClock(TextView clockFace, String format){
        if (clockFace==null) return false;
        if(clocks.contains(clockFace)){
            // old clockFace
            clocks.get(clocks.indexOf(clockFace)).format=format;
        } else {
            // new clockFace
            clocks.add(new Clock(clockFace, format));
        }
        return true;
    }
    /**
     * remove a clock from the updating list
     * @param clockFace
     * @return
     */
    public static boolean unRegisterClock(TextView clockFace){
        if (clockFace==null) return false;
        if(clocks.contains(clockFace)){
            // found clockFace
            clocks.remove(clocks.indexOf(clockFace));
        } else {
            // not found clockFace
            return false;
        }
        return true;
    }
    /**
     * put in the "place" the actual date/time in the appropriate "format"
     * @param place
     * @param format
     */
    public static void showActualTimeDate(Clock clock){
        if (clock.clockFace==null) return;
        if (clock.clockFace.getVisibility()!=TextView.VISIBLE) return;
        Date thisDate=new Date();
        SimpleDateFormat df=new SimpleDateFormat(clock.format);
        clock.clockFace.setText(df.format(thisDate));
    }
    /**
     * start the ticking for all clocks
     * @param step the tick interval
     */
    public static void start(long step) { 
        refreshStep=step;
        handler.removeCallbacks(mUpdateTimeTask);
        handler.postDelayed(mUpdateTimeTask, 0);
    }
    /**
     * Stopping ticking all clocks (not removing them)
     * the calling could be put somewhere in onStop
     */
    public static void stop() { 
        handler.removeCallbacks(mUpdateTimeTask);
    }
}

答案 2 :(得分:0)

好吧,我想到了一个AsyncTask,它是从计时器的run()方法中执行的。