Android 每两分钟刷新一次列表视图
我正在尝试每两秒钟刷新一次我的Android 每两分钟刷新一次列表视图,android,listview,Android,Listview,我正在尝试每两秒钟刷新一次我的列表视图。首先执行loadingTask,适配器从arraylist chat填充ListView。然后每隔两秒钟,就会调用刷新适配器加载任务,并作为连续循环执行,然后再次填充arraylist聊天。我需要从ArrayList中删除以前的项目,只保留新项目。请了解如何实现这种道路通行功能。目前,我的数组一直以循环的形式填充以前httpcall中的项 int refreshInterval =2000; //set refresh interval class lo
列表视图。首先执行loadingTask
,适配器从arraylist chat填充ListView。然后每隔两秒钟,就会调用刷新适配器加载任务,并作为连续循环执行,然后再次填充arraylist聊天。我需要从ArrayList
中删除以前的项目,只保留新项目。请了解如何实现这种道路通行功能。目前,我的数组一直以循环的形式填充以前httpcall中的项
int refreshInterval =2000; //set refresh interval
class loadingTask extends AsyncTask<Void, Void,Void> {
//arraylist
chat = new ArrayList<Chat>();
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
...
for (int i=0;i<room_length;i++){
JSONObject obj_value = responseArray.getJSONObject(i);
if (obj_value.has("@id"))
id=obj_value.getString("@id");
if (obj_value.has("@version"))
version=obj_value.getString("@version");
...
chatAdapter = new ChatAdapter(m,chat);
msgList.setAdapter(chatAdapter); //set data to listview
chatAdapter.notifyDataSetChanged();//update changes
msgList.setSelection(msgList.getAdapter().getCount()-1);
handler.postDelayed(refreshView, refreshInterval);
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
return null;
}
public static class refreshAdapterLoadingTask extends AsyncTask<Void, Void,Void> {
private String isMarketingMessage;
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
//if(chat!=null)
//chat.clear();
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
try {
...
for (int i=0;i< responseArray.length();i++){
JSONObject obj_value = responseArray.getJSONObject(i);
if (obj_value.has("@id"))
id=obj_value.getString("@id");
if (obj_value.has("@version"))
version=obj_value.getString("@version");
if (obj_value.has("@content"))
content=obj_value.getString("@content");
if (obj_value.has("@sentDate"))
sentDate=obj_value.getString("@sentDate");
if (obj_value.has("player"))
player=obj_value.getString("player");
chat.add(new Chat(isMarketingMessage, id, version,content, sentDate, player));
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//remove previous arraylist values
// for (int i=0;i<previousArrayListSize;i++){
// chat.remove(i);
//}
handler.postDelayed(refreshView, refreshInterval);
chatAdapter = new ChatAdapter(m,chat);
chatAdapter.notifyDataSetChanged();
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
return null;
}
}
}
尝试以下代码
new Handler().postDelayed(new Runnable() {
@Override
//automatically disappear after given time and start game
public void run() {
//call your loadingTask here
}
}, refreshInterval);
并在post execute中调用相同的方法
希望这将帮助您尝试以下代码
new Handler().postDelayed(new Runnable() {
@Override
//automatically disappear after given time and start game
public void run() {
//call your loadingTask here
}
}, refreshInterval);
// Using Timer and Runnable method you can list refresh
Timer t = new Timer();
t.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
// your process
process();
}
}, 0, 2000);
// Process method
private void process() {
new Thread(new Runnable() {
public void run() {
try {
// LiveScoredata();
} catch (Exception e) {
}
runOnUiThread(new Runnable() {
public void run() {
try {
//your list fill hear
adapter.notifyDataSetChanged();
} catch (Exception e) {
}
}
});
}
}).start();
}
并在post execute中调用相同的方法
希望这将对您有所帮助我只需要从arraylist中删除以前设置的项而无需用户通知arraylist已清除我只需要从arraylist中删除以前设置的项而无需用户通知arraylist已清除adapter notifydatasetchanged清除arraylist并填写arraylist检查更新我将您的代码与您告诉我的操作一致但如果我清除了chat arraylist,则在更新之前,在清除数组列表之后,所有记录都将消失。在列表或适配器中填充数据。notifyDataSetChanged();使用此选项。然后如果(chat!=null)chat.clear();然后填充适配器并在listview中设置适配器。当chat arraylist变为null时,arrayadapter也将变为null上述适配器notifydatasetchanged clear arraylist and fill arraylist检查更新我按照您告诉我的操作将您的代码放入,但如果我清除chat arraylist,则在更新之前所有记录都将消失清除数组列表填充数据之后在列表或适配器中。notifyDataSetChanged();使用此选项。然后如果(chat!=null)chat.clear();然后填充适配器并在listview中设置适配器。当聊天室arraylist变为空时,arrayadapter也将变为空
// Using Timer and Runnable method you can list refresh
Timer t = new Timer();
t.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
// your process
process();
}
}, 0, 2000);
// Process method
private void process() {
new Thread(new Runnable() {
public void run() {
try {
// LiveScoredata();
} catch (Exception e) {
}
runOnUiThread(new Runnable() {
public void run() {
try {
//your list fill hear
adapter.notifyDataSetChanged();
} catch (Exception e) {
}
}
});
}
}).start();
}