更新列表的用户界面与计时器 [英] Updating the List UI with Timer

查看:96
本文介绍了更新列表的用户界面与计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试更新列表视图timer.I已经实现了Android UI定时器​​教程,但我的问题是如何使用它的列表视图,我需要一定的时间间隔后更新列表中的每一行。如何处理程序会更新列表的每一行(即假设一个TextView是我会显示更新的值eachrow内。)

公共类ListAdapter扩展了BaseAdapter {

 私人列表<字符串>消息;
私人语境mContext;

公共ListAdapter(上下文的背景下,列表与LT;字符串>消息){
    this.messages =消息;
    mContext =背景;
}

@覆盖
公众诠释getCount将(){
    返回messages.size();
}

@覆盖
公共对象的getItem(INT位置){
    返回messages.get(位置);
}

@覆盖
众长getItemId(INT指数){
    返回指数;
}

@覆盖
公共查看getView(INT索引,视图convertView,ViewGroup中的ViewGroup){

        TextView的时间为新的TextView(mContext);

            // UDPATE的time.setText()使用定时器
    convertView =(查看)时间

    返回convertView;
}
 

解决方案

创建扩展处理程序类customHandler类,并从定时器run()方法调用处理器的sendEmptyMessage()。

在处理程序类重写的handleMessage并更新您的列表视图值并调用adapter.notifyDataSetChanged(),这将刷新列表视图与更新的值。

下面是示例code:

 定时器定时=新的Timer();
CustomTimerTask customTimerTask =新CustomTimerTask();
customTimerTask.run();
timer.scheduleAtFixedRate(customTimerTask,1000,1000);

类CustomTimerTask扩展的TimerTask {

    @覆盖
    公共无效的run(){
        myHandler.sendEmptyMessage(0);
    }
}

    类CustomHandler扩展处理程序{


    @覆盖
    公共无效的handleMessage(信息MSG){
        super.handleMessage(MSG);
        ///做你的列表视图更新的东西
 }
 

}

I am trying to update the list view with timer.I have implemented the android UI timer tutorial but my problem is how to use it for List view where i need to update the each row of list after a certain interval. how does the handler will update the each row of the list(i.e suppose a textview is inside the eachrow where i'll display the updated values.)

public class ListAdapter extends 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;
}   

解决方案

Create a customHandler class that extends Handler class and call the sendEmptyMessage() of handler from the timer run() method.

In the Handler class override the handleMessage and update your listview values and call adapter.notifyDataSetChanged() which will refresh the listview with the updated values.

Here is the sample code :

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
 }

}

这篇关于更新列表的用户界面与计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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