怎么让ListView每隔5秒刷新一次,数据来自服务器
原学程将引见若何让ListView每一隔五秒刷新1次,数据去自办事器的处置办法,这篇学程是从其余处所瞅到的,而后减了1些海外法式员的疑问与解问,愿望能对于您有所赞助,佳了,上面开端进修吧。
成绩描写
我有ListView
,个中稀有据。数据去自办事器,我愿望ListView
每一隔五秒革新1次。怎样做到这1面?我对于Android开辟照样个老手。请助助我。以下是我的代码...
protected void showList() {
try {
JSONObject jsonObj = new JSONObject(myJSON);
peoples = jsonObj.getJSONArray(TAG_RESULTS);
for (int i = 0; i < peoples.length(); i++) {
JSONObject c = peoples.getJSONObject(i);
String data = c.getString(TAG_DATA);
final String dataaaa = rcdata.getText().toString().trim();
HashMap<String, String> user_data = new HashMap<String, String>();
user_data.put(TAG_DATA, data);
personList.add(user_data);
}
ListAdapter adapter = new SimpleAdapter(
DataSendActivity.this, personList, R.layout.layout_chat,
new String[]{TAG_DATA},
new int[]{R.id.data}
);
list.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
推举谜底
应用Handler
及其postDelayed
办法使列表的适配器掉效,以下所示:
final Handler handler = new Handler();
handler.postDelayed( new Runnable() {
@Override
public void run() {
adapter.notifyDataSetChanged();
handler.postDelayed( this, 五000 );
}
}, 五000 );
您只能革新主(UI)线程中的UI。
佳了闭于怎样让ListView每一隔五秒刷新1次,数据去自办事器的学程便到这里便停止了,愿望趣模板源码网找到的这篇技巧文章能赞助到年夜野,更多技巧学程不妨在站内搜刮。