自动更新和刷新主/详细信息流

时间:2015-11-10 15:51:38

标签: android android-studio master-detail

我创建了一个主/详细流应用程序,它连接到一个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(),但我不知道什么时候调用它。我该怎么做?

编辑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.
}

1 个答案:

答案 0 :(得分: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是首选方法。