0

Ideally I want to implement

Method level synchronization where whole method is synchronized every 1minute

Not

Block level synchronization where only some set of statements are synchronized

But I don't know why when my new runnable handler execute the method at the set interval. It parses unchanged json data again and again populating my listview in a redundant manner.

How can I implement a handler that will only parse data from json that has changed/updated from previous 60seconds, without repeating data on list view?

    private class MyAsyncTask extends AsyncTask<Void, Void, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        dialog = new ProgressDialog(RegionListActivity.this);
        dialog.setMessage("Please wait..");
        dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        dialog.show();
    }

    @Override
    protected String doInBackground(Void... params) {
        String url_structure = GlobalData.MAIN_URL + sessionId
                + "/user_session/?sections=app-basic&company_id="
                + companyId;
        String response_structure = CallWebService
                .getResponseOfUrl(url_structure);
        parseRegionList(response_structure);

        String url_unit = GlobalData.MAIN_URL + sessionId
                + "/reporting/system/unit_status/?sections&company_id="
                + companyId;
        String response_unit = CallWebService.getResponseOfUrl(url_unit);
        parseUnitStatus(response_unit);
        return null;

    }

    @SuppressWarnings("null")
    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        dialog.dismiss();

        if (!alRegions.isEmpty() && !alParents.isEmpty()) {
            expandableAdapter = new ExpandableListAdapter(
                    RegionListActivity.this, alParents);
            lvExpandableRL.setAdapter(expandableAdapter);

            GlobalData.alAssetsGlobal = hashMapAssets;
            GlobalData.alComponetsGlobal = hashMapComponents;

        }

        else {
            lvExpandableRL.setVisibility(View.GONE);
            tvNoRecordsRL.setVisibility(View.VISIBLE);
        }

        tvCompanyNameRL.setText(GlobalData.COMPANY_NAME);
        tvActiveAlarmsRL.setText(GlobalData.ACTIVE_ALARMS);
        tvUnitsOfflineRL.setText(GlobalData.OFFLINE_COUNT);

        /*
         * try { if(alUnitNumber.size()>0 && alUnitNumber != null) {
         * al_unit_snumber=alUnitNumber;
         * lvUnitsOffline.setVisibility(View.VISIBLE); adapter= new
         * UnitListAdapter(RegionListActivity.this, alUnitNumber);
         * lvUnitsOffline.setAdapter(adapter); } else {
         * lvUnitsOffline.setVisibility(View.GONE);
         * tvNoRecordsUnitListOffline.setVisibility(View.VISIBLE); } } catch
         * (Exception e) { // TODO Auto-generated catch block
         * e.printStackTrace(); }
         * 
         * if(alUnitStatus.size()>0 && alUnitStatus != null)
         * 
         * alUnitStatus=(ArrayList<DataUnits>)getArguments()
         * .getSerializable(WebElement.RECORDS); if(alUnitStatus.size()>0 &&
         * alUnitStatus != null) { al_unit_snumber=alUnitNumber;
         * lvUnitsOffline.setVisibility(View.VISIBLE); adapter= new
         * UnitListAdapter(RegionListActivity.this, alUnitStatus);
         * lvUnitsOffline.setAdapter(adapter); } else {
         * lvUnitsOffline.setVisibility(View.GONE);
         * tvNoRecordsUnitListOffline.setVisibility(View.VISIBLE); }
         */

    }
    new Handler().postDelayed(new Runnable() {
        public void run() {
            // call JSON methods here
            new MyAsyncTask().execute();
        }
    }, 60000);

}
Cockpit Aliens
  • 411
  • 6
  • 18
  • new Handler().postDelayed(new newRunnable() { public void run() { // call JSON methods here new LoadStructureTask().execute(); } }, 60000 ); JSONObject jOb = json; // json – Cockpit Aliens Nov 27 '14 at 06:19
  • http://stackoverflow.com/questions/14376470/scheduling-recurring-task-in-android – Ravi Shanker Yadav Nov 27 '14 at 06:53
  • Hi Ravi thanks for sharing. But the solution at the link does not solve my problem. My handler does sync every 1minute the issue as i described in my question is that when it refreshes it populates the list view repeating the json data over and over again instead of just refreshing same list view items with new values if they have changed. – Cockpit Aliens Nov 27 '14 at 07:01

0 Answers0