0

I created a master/detail flow application, that is connecting to a url to get a JSON. This JSON is then parsed, to get a list of commands and all the details of it. I would like to refresh the item list without any action from the user. I know there is a simple way to create the list with the JSON data, but how can i update it every 10 seconds simply ?

EDIT :

        Timer timerTask = new Timer();
    timerTask.schedule(new TimerTask() {
        @Override
        public void run() {
            try{
                DummyContent.ITEMS.clear();
                GetURLContentTask eu = new GetURLContentTask();

                String JSONString = eu.execute(url).get();
                System.out.println("debug h " + JSONString);
                JSONArray jArray = new JSONArray(JSONString);

                System.out.println("debug *****JARRAY*****"+jArray.length());
                if (!jArray.equals(null)) {
                    for(int i=0;i<jArray.length();i++){
                        JSONObject json_data = jArray.getJSONObject(i);
                        System.out.println("debug unique id : " + json_data.getString("unique_id") +
                                ", point de vente : " + json_data.getString("pdv") +
                                ", panier : " + json_data.getString("panier"));
                        DummyContent.addItem(new DummyContent.DummyItem(json_data.getString("unique_id"), json_data.getString("pdv"), json_data.getString("panier")));
                        //  .notifyDataSetChanged();
                    }
                }
            }catch (Exception e){System.out.println("error: "+e);}
        }
    }, 10000, 10000);

I have to use notifyDataSetChanged(), but i don't know whento call it. How do I do this ?

EDIT 2 : This is the ItemListFragment.java :

    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // TODO: replace with a real list adapter.
    setListAdapter(new ArrayAdapter<DummyContent.DummyItem>(
            getActivity(),
            android.R.layout.simple_list_item_activated_1,
            android.R.id.text1,
            DummyContent.ITEMS));
}

And this is my ItemListActivity :

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_commande_app_bar);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    toolbar.setTitle(getTitle());


    Timer timerTask = new Timer();
    timerTask.schedule(new TimerTask() {
        @Override
        public void run() {
            try{
                DummyContent.ITEMS.clear();
                GetURLContentTask eu = new GetURLContentTask();

                String JSONString = eu.execute(url).get();
                System.out.println("debugTT h " + JSONString);
                JSONArray jArray = new JSONArray(JSONString);

                System.out.println("debugTT *****JARRAY*****"+jArray.length());
                if (!jArray.equals(null)) {
                    for(int i=0;i<jArray.length();i++){
                        JSONObject json_data = jArray.getJSONObject(i);
                        System.out.println("debugTT unique id : " + json_data.getString("unique_id") +
                                ", point de vente : " + json_data.getString("pdv") +
                                ", panier : " + json_data.getString("panier"));
                        DummyContent.addItem(new DummyContent.DummyItem(json_data.getString("unique_id"), json_data.getString("pdv"), json_data.getString("panier")));
                        CommandeListFragment.adapter.notifyDataSetChanged();
                    }
                }
            }catch (Exception e){System.out.println("error: "+e);}
        }
    }, 10000, 10000);



    if (findViewById(R.id.commande_detail_container) != null) {
        // The detail container view will be present only in the
        // large-screen layouts (res/values-large and
        // res/values-sw600dp). If this view is present, then the
        // activity should be in two-pane mode.
        mTwoPane = true;

        // In two-pane mode, list items should be given the
        // 'activated' state when touched.
        ((CommandeListFragment) getSupportFragmentManager()
                .findFragmentById(R.id.commande_list))
                .setActivateOnItemClick(true);
    }

    // TODO: If exposing deep links into your app, handle intents here.
}
Dhost
  • 53
  • 9
  • Please check this one http://stackoverflow.com/questions/14376470/scheduling-recurring-task-in-android – Rahul Chaurasia Nov 10 '15 at 16:03
  • I wasn't precise enough. What i want to know is not how to call a method or a function every 10 seconds, but which one to call, and how, to refresh my item list. – Dhost Nov 10 '15 at 16:06

1 Answers1

0

For your case, you can use Timer or ScheduledThreadPoolExecutor

Here goes the code -

    private void retrieveDataFromServer(){
        // your code to download and parse data

    }

You should fetch, parse and set the data to your adapter in your master fragment not in your activity.

            Timer timerTask = new Timer();
            timerTask.schedule(new TimerTask() {
                @Override
                public void run() {

                    // Get the new data from the server 
                GetURLContentTask eu = new GetURLContentTask();

                String JSONString = eu.execute(url).get();
                System.out.println("debug h " + JSONString);
                JSONArray jArray = new JSONArray(JSONString);

                System.out.println("debug *****JARRAY*****"+jArray.length());
                if (!jArray.equals(null)) {
                    for(int i=0;i<jArray.length();i++){
                        JSONObject json_data = jArray.getJSONObject(i);
                        System.out.println("debug unique id : " + json_data.getString("unique_id") +
                                ", point de vente : " + json_data.getString("pdv") +
                                ", panier : " + json_data.getString("panier"));
                        DummyContent.addItem(new DummyContent.DummyItem(json_data.getString("unique_id"), json_data.getString("pdv"), json_data.getString("panier")));
                       // Clear the older data and set the new data

                    adapter.clear();
                    adapter.addAll(DummyContent);
                    adapter.notifyDataSetChanged();
                    }
                } 


                }
            }, 10000, 10000);

I've given example using the Timer class but the ScheduledThreadPoolExecutor is the preferred way.

Rahul Chaurasia
  • 1,601
  • 2
  • 18
  • 37