当数据来自服务器时,如何使ListView在每5秒后刷新一次

时间:2016-05-24 04:41:19

标签: java android listview refresh

我有ListView有数据。数据来自服务器,我希望{5}每隔5秒后更新ListView。这该怎么做?我是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();
    }
}

2 个答案:

答案 0 :(得分:1)

使用postDelayed及其final Handler handler = new Handler(); handler.postDelayed( new Runnable() { @Override public void run() { adapter.notifyDataSetChanged(); handler.postDelayed( this, 5000 ); } }, 5000 ); 方法使列表的适配器无效,如下所示:

library(ggplot2)
library(scales)
library(ggmap)

setwd("<path to df.csv file>")
df <- read.csv("df.csv")

ggplot(df, aes(x=long, y= lat)) + 
  geom_polygon(aes(x=long, y= lat, group= Site1, fill = mean),
               color = "black",
               size = 0.1)+
  scale_fill_distiller(name=bquote(atop("Mean Annual",
                                        "Concentration" ~ (mg~L^{-1}))),
                       palette = 
                         "OrRd"
                       , 
                       breaks = pretty_breaks(n = 5),
                       direction = 1)+
  facet_wrap(~year, ncol=4)+
  theme_nothing(legend = TRUE)

您必须只更新主(UI)线程中的UI。

答案 1 :(得分:0)

您可以看到此问题并发表评论How to refresh/Update Serialize Java Object in sharedPreferences。用户想要的一样。然而,对于那种在一分钟内调用asyncTask的问题,使用带有服务的加载器是好的。另外还有一个示例项目,您可以在Playstore中查看此https://github.com/Michenux/YourAppIdea,该项目检查服务器的数据更改。