Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/190.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 从远程服务器更新Listview数据_Android_Android Asynctask_Runnable - Fatal编程技术网

Android 从远程服务器更新Listview数据

Android 从远程服务器更新Listview数据,android,android-asynctask,runnable,Android,Android Asynctask,Runnable,您好,我正在尝试从远程服务器SPHP和mysql更新活动的listview数据,我使用Async task方法从服务器调用数据,但我每2秒调用一次Async task方法。我就是这样做的 /** * The runnable method that is called every 2 seconds. */ Runnable run= new Runnable() { public void run() {

您好,我正在尝试从远程服务器SPHP和mysql更新活动的listview数据,我使用Async task方法从服务器调用数据,但我每2秒调用一次Async task方法。我就是这样做的

    /**
     * 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

            }
        }

    }

这个逻辑是正确的,但我认为这不是更新同一活动的列表视图的最有效的方法,解决这个问题最有效的方法是什么?提前感谢。

您无需创建新适配器。只需用新数据更新当前适配器。最简单的方法是清除所有数据并添加所有新数据:

ListView lView = (ListView) findViewById(R.id.lists);
ApplicationAdapter adapter = (ApplicationAdapter) lView.getAdapter();

adapter.clear();
adapter.addAll(apps);

您的UI将相应更新。

每2秒实际上是一段很短的时间,AYSNTASK可能需要更多的时间,根据互联网速度,尝试稍微增加时间(可能5-8秒),以确保aysnc在启动新的AYSNTASK之前实际完成。这将清除所有数据,而不显示任何数据