android如何使ListView每5秒刷新一次

时间:2016-05-24 15:35:47

标签: java android listview

我有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();
    }
}
  

也试过这个onCreate

    final Handler handler = new Handler();
handler.postDelayed( new Runnable() {

    @Override
    public void run() {
        adapter.notifyDataSetChanged();
        handler.postDelayed( this, 5000 );
    }
}, 5000 );

我使用此方法获取数据

public void getData() {
    class GetDataJSON extends AsyncTask<String, Void, String> {
        //String recID = ;
        //String userID = email;
        @Override
        protected String doInBackground(String... params) {
            DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
            HashMap<String, String> user = db.getUserDetails();

            //String name = user.get("name");
           // Semail = user.get("email");

            String semail  = user.get("email");
            final String remail = rremail;

            HttpPost httppost = new HttpPost("http://samplechatapp.gear.host/myphpfile.php?sender_email="+semail+"&reciver_email="+remail+"&fatch_server_data=true");

            // Depends on your web service
            httppost.setHeader("Content-type", "application/json");

            InputStream inputStream = null;
            String result = null;
            Log.i("","processing entity");
            try {
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();

                inputStream = entity.getContent();
                // json is UTF-8 by default
                BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
                StringBuilder sb = new StringBuilder();

                String line = null;
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                    Log.i("",line);
                }
                result = sb.toString();
                Log.i("",result);
            } catch (Exception e) {
                // Oops
            } finally {
                try {
                    if (inputStream != null) inputStream.close();
                } catch (Exception squish) {
                }
            }
            return result;
        }

        @Override
        protected void onPostExecute(String result) {
            myJSON = result;
            showList();
        }
    }
    GetDataJSON g = new GetDataJSON();
    g.execute();
}

但是当我打开listview应用程序崩溃时,请帮助我,我是android的新手

2 个答案:

答案 0 :(得分:0)

您应该在收到第一个api电话的响应后调用api。您可notifyDataSetChanged()张贴该内容。

答案 1 :(得分:0)

只要数据发生变化,请致电notifyDataSetChanged()。 此外,请避免使用ListView而不是使用RecyclerView开始。