来自 JSON 的数据不会刷新


Data from JSON does not refresh

我正在Android中开发一个JSON解析应用程序,我的服务器上有一个php脚本,用于从mysql数据库获取所有数据并编码为json,应用程序与服务器联系,并使用AsyncTaskLoader使用来自JSON的数据填充ListView。它有效,但如果我删除所有数据库记录,然后重新启动我的应用程序,ListView 仍然显示旧数据。即使我按下了调用AsyncTaskLoader但仍然是旧数据的刷新按钮。我不知道为什么请帮助我。

谢谢P/s:对不起,我的英语^^不好

对不起,我忘记了代码。来吧

TVSchedFragment.java

public class TVSchedFragment extends ListFragment {
    private TVSchedAdapter adpt;
    ListView lView;
    // JSON Node names
    private static final String TAG_SCHEDULE = "Sched";
    private static String url = "http://192.168.20.205/DOM/app/json.php";
    String jsonStr;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_tvsched, container,
                false);
        lView = (ListView) rootView.findViewById(android.R.id.list);
        adpt = new TVSchedAdapter(new ArrayList<TVSchedModel>(), getActivity());
        lView.setAdapter(adpt);
        (new AsyncListViewLoader()).execute();
        return rootView;
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }
    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.menu_tvsched, menu);
        super.onCreateOptionsMenu(menu, inflater);
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.menu_tvsched_refresh:
            (new AsyncListViewLoader()).execute();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }
    private class AsyncListViewLoader extends
            AsyncTask<String, Void, List<TVSchedModel>> {
        private final ProgressDialog dialog = new ProgressDialog(getActivity());
        @Override
        protected void onPostExecute(List<TVSchedModel> result) {
            super.onPostExecute(result);
            dialog.dismiss();
            adpt.setItemList(result);
            adpt.notifyDataSetChanged();
        }
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            dialog.setMessage("Downloading schedule...");
            dialog.show();
        }
        @Override
        protected List<TVSchedModel> doInBackground(String... params) {
            List<TVSchedModel> result = new ArrayList<TVSchedModel>();
            ServiceHandler sh = new ServiceHandler();
            // Making a request to url and getting response
            jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
            Log.d("Response: ", "> " + jsonStr);
            try {
                JSONObject jsonObj = new JSONObject(jsonStr);
                // Getting JSON Array node
                JSONArray contacts = jsonObj.getJSONArray(TAG_SCHEDULE);
                for (int i = 0; i < contacts.length(); i++) {
                    result.add(convertSched(contacts.getJSONObject(i)));
                }
                return result;
            } catch (Throwable t) {
                t.printStackTrace();
            }
            return null;
        }
        private TVSchedModel convertSched(JSONObject obj)
                throws JSONException {
            String name = obj.getString("Program");
            String desc = obj.getString("Description");
            String time = obj.getString("dateTimeString");
            String chan = obj.getString("Channel");
            return new TVSchedModel(time, name, desc, chan);
        }
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        adpt.clear();
    }
}

服务处理程序.java

public class ServiceHandler {
    static String response = null;
    public final static int GET = 1;
    public final static int POST = 2;
    public ServiceHandler() {
    }
    /*
     * Making service call
     * @url - url to make request
     * @method - http request method
     * */
    public String makeServiceCall(String url, int method) {
        return this.makeServiceCall(url, method, null);
    }
    /*
     * Making service call
     * @url - url to make request
     * @method - http request method
     * @params - http request params
     * */
    public String makeServiceCall(String url, int method,
            List<NameValuePair> params) {
        try {
            // http client
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpEntity httpEntity = null;
            HttpResponse httpResponse = null;
            // Checking http request method type
            if (method == POST) {
                HttpPost httpPost = new HttpPost(url);
                // adding post params
                if (params != null) {
                    httpPost.setEntity(new UrlEncodedFormEntity(params));
                }
                httpResponse = httpClient.execute(httpPost);
            } else if (method == GET) {
                // appending params to url
                if (params != null) {
                    String paramString = URLEncodedUtils
                            .format(params, "utf-8");
                    url += "?" + paramString;
                }
                HttpGet httpGet = new HttpGet(url);
                httpResponse = httpClient.execute(httpGet);
            }
            httpEntity = httpResponse.getEntity();
            response = EntityUtils.toString(httpEntity);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return response;
    }
}

如果使用 ArrayAdapter 显示列表视图,请在 finish() 上清除数组列表,或者在刷新列表时清除数组列表。清除上一个列表存储更新的数据并调用 adapter.notifyDataChanged();