如何在android中每5秒更新一次监控窗口(listview)
我从监控窗口的url获取数据,该窗口显示访问站点的访问者列表,以及他们的ip、会话id、到站点的生存时间,访问次数以及该url中的访问状态。我必须在列表视图中显示数据,并在每5秒后获取新数据。如何使设置从url获取数据的列表视图在每5秒后更新从服务器获取的数据(意味着每5秒后将新数据添加到列表) 有人能帮我吗 我使用的更新方法如下如何在android中每5秒更新一次监控窗口(listview),android,Android,我从监控窗口的url获取数据,该窗口显示访问站点的访问者列表,以及他们的ip、会话id、到站点的生存时间,访问次数以及该url中的访问状态。我必须在列表视图中显示数据,并在每5秒后获取新数据。如何使设置从url获取数据的列表视图在每5秒后更新从服务器获取的数据(意味着每5秒后将新数据添加到列表) 有人能帮我吗 我使用的更新方法如下 Handler handler = new Handler(); Runnable updater = new Runnable() { pub
Handler handler = new Handler();
Runnable updater = new Runnable() {
public void run() {
/*
* Update the list
*/
getVisitorDetailFromServer();
try {
Log.i("UPDATE", "Handler called");
// searchAdapter = getFeed(URL);
handler.postDelayed(this, 3000);
} catch(Exception e) {
Log.e("UPDATE ERROR", e.getMessage());
}
}
};
updater.run();
我现在正在使用此方法正确获取数据,但问题是应用程序崩溃一段时间后,java.lang.indexoutofboundsexception会发生。请尝试在列表视图中使用notifydatasetchange。运行一个线程,在该线程中只需执行adater.notifydatasetchange。尝试以下方法: 1将数据放入数组、数据库或其他与listview的适配器相关的文件中 2创建一个处理程序,并使用sendMessageDelaed(Message,5000)或postDelayed(Runnable,5000)组成一个循环 3调用适配器.notifyDatasetChange();在可运行或可处理的消息中
总之,您应该做的是每5秒更新数据集并调用notifydatasetchange(也可以使用TimerTask或其他方法)查看包含您的数据的数据结构(arraylist或其他)是否正在更改,即您正在执行的操作是“您的数组列表”=new arraylist()
每次从服务器获取新数据时,“notifyDataSetChanged()”都不起作用,
它所做的是通知适配器包含数据的arraylist已更新,然后适配器尝试从同一地址刷新数据。
但在您的情况下,数据结构的地址会发生更改。
因此,如果您这样做,您需要在“update()”中再次设置适配器计时器任务的方法。要使用定时计时器,可以使用以下代码:
private Timer myTimer;
myTimer = new Timer();
myTimer.schedule(new TimerTask() {
@Override
public void run() {
TimerMethod();
}
}, 0, 10000);
}
}
private void TimerMethod()
{
//This method is called directly by the timer
//and runs in the same thread as the timer.
//We call the method that will work with the UI
//through the runOnUiThread method.
this.runOnUiThread(Timer_Tick);
}
private Runnable Timer_Tick = new Runnable() {
public void run() {
//This method runs in the same thread as the UI.
//Do something to the UI thread here
}
};
到目前为止,您一直在尝试什么?您面临什么问题?从getVisitorDetailFromServer()我从正在listview中添加的服务器获取数据。设置数据后,我将续订ArrayList。我的应用程序在某个时间后崩溃。TimerTask TimerTask=new TimerTask(){@Override public void run(){updateList();} }; 时间表(timerTask,5000);我希望更新我的listview,每当从服务器获取新数据时,在5秒后添加新项目谢谢。我使用了此选项,但我的listview没有刷新,我在5秒后从服务器获取新数据。我希望在获取新数据后,listview立即用新数据更新,就像监控窗口中任何更新了访客列表的livechat一样几秒钟后自动执行。每次获取新数据调用notifyDatasetChange()都不起作用?在哪里保存数据,尝试调用setAdapter(adapter);获取新数据后,我再次使用此方法,正如您所说,并在更新方法中再次设置适配器。我的应用程序正在崩溃,同时更新arraylist,但我的应用程序正在崩溃。您可以通过www.pastebin.com或