0

I'm developing a JSON Parsing app in Android, I have a php script on my server to fetch all data from mysql database and encode to json, the app contact to the server and populate the ListView with the data from JSON using AsyncTaskLoader . It's worked but if I drop all database records, then start my app again, the ListView still shows old data. Even I press the refresh button that call AsyncTaskLoader but still old data. I don't know why please help me.

Thank you P/s: Sorry for my bad English ^^

Sorry, I forgot the code. Here we go

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

ServiceHandler.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;

    }
}
Minh Phan
  • 47
  • 6

1 Answers1

0

If you are using ArrayAdapter to show list view clear the array list on finish() or whenever you refresh the list.clear the previous list store the updated data and call adapter.notifyDataChanged();.

Yuvaraja
  • 715
  • 6
  • 22