第一件事是使用 postdelayed() 不会重复运行您的代码如果您想以重复模式运行代码,请使用此代码。这将在每 5 秒后运行一次
ScheduledExecutorService scheduleTaskExecutor = Executors.newScheduledThreadPool(5);
/*This schedules a runnable task every second*/
scheduleTaskExecutor.scheduleAtFixedRate(new Runnable() {
public void run()
{
runOnUiThread(new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
new GetContacts().execute();
}
});
}
}, 0, 5, TimeUnit.SECONDS);
您所遵循的代码每次都会在 postExecute() 中创建 SimpleAdapter 的新实例,因此您将一次又一次地看到相同的数据。因此,如果您想更新您的适配器,请创建 SimpleAdapter 实例作为类成员并用这个替换 postExecute()
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
/**
* Updating parsed JSON data into ListView
* */
if(adapter == null)
{
adapter = new SimpleAdapter(
MainActivity.this, contactList,
R.layout.list_item, new String[] { TAG_NAME, TAG_EMAIL,
TAG_PHONE_MOBILE }, new int[] { R.id.name,
R.id.email, R.id.mobile });
setListAdapter(adapter);
}
else
{
adapter.notifyDataSetChanged();
}
}
现在这将更新适配器,但会添加相同的联系人