0
你好,我正在嘗試更新我的列表視圖數據上來自遠程服務器(PHP和MySQL)的活動,我使用異步任務方法從服務器調用數據,但我打電話異步任務方法每2秒。這就是我要做的事更新來自遠程服務器的列表視圖數據
/**
* The runnable method that is called every 2 seconds.
*/
Runnable run= new Runnable() {
public void run() {
new Comments(false).execute();
handler.postDelayed(this, 2000);
}
};
runOnUiThread(run);
/**
* Async Task method for calling data from the remote servers
*/
public class Comments extends AsyncTask<String, String, JSONArray> {
public Comments(boolean showLoading) {
super();
// do stuff
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected JSONArray doInBackground(String... aids) {
//This gets all the information unread from the server
json = Function.Comments();
return json;
}
@Override
protected void onPostExecute(JSONArray json) {
List<Application> apps = new ArrayList<Application>();
if (json != null) {
try {
for (int i = 0; i < json.length(); i++) {
JSONObject jsons = json.getJSONObject(i);
Application app = new Application();
//Values from the remote database
app.setMsgID(jsons.getString("msgID"));
apps.add(app);
}
ApplicationAdapter adapter = new ApplicationAdapter(context,apps);
ListView lView = (ListView) findViewById(R.id.lists);
lView.setAdapter(adapter);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else {
//stuff
}
}
}
這種邏輯正常工作,但我認爲這是不更新的同一個活動列表視圖最effiicient方式,什麼是最有效的工作了這一點?提前致謝。
每2秒實際上是一段時間,aysncTask可能需要更多的時間,取決於網速,嘗試增加一點時間(可能5-8秒)以確保aysnc在開始新的練習之前實際完成aysncTask。 –