2015-11-10 54 views
0

我創建了主/明細流應用程序,即連接到URL以獲取JSON。然後解析此JSON,以獲取命令列表及其所有細節。我想刷新項目列表,而不需要用戶採取任何行動。我知道有一種簡單的方法可以用JSON數據創建列表,但我如何每10秒更新一次?自動更新和刷新主/明細流

編輯:

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

我必須使用notifyDataSetChanged(),但我不知道whento調用它。我該怎麼做呢 ?

編輯2: 這是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)); 
} 

這是我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. 
} 
+0

請檢查這一個http://stackoverflow.com/questions/14376470/scheduling-recurring-task-in-android –

+0

我不夠精確。我想知道的不是如何每10秒調用一次方法或函數,而是調用哪一個方法或函數,以及如何刷新我的項目列表。 – Dhost

回答

0

對於你的情況,你可以使用TimerScheduledThreadPoolExecutor

這裏去代碼 -

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

    } 

您應該將數據提取,解析並設置到主分段中的適配器,而不是在您的活動中。

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

我已經給例如使用Timer類,但ScheduledThreadPoolExecutor是首選的方式。

+0

我更新了我的問題。我不知道如何使用notifySetDataChanged() – Dhost

+0

一旦你有最新的數據,你可以調用yourAdapter.notifyDataSetChange() –

+0

我所有的是一個ArrayAdapter,根本沒有簡單的適配器..我如何創建這個適配器? – Dhost