在同一个Activity中移动数据

时间:2018-03-30 15:36:34

标签: java android

在我的应用程序中,我从Json中检索了一些数据,我存储在昨天今天和明天的字符串中,现在问题我需要的数据将是我的标签的标题。它都是同一活动的一部分,我不能使用Intent或Bundle来获取和发送它。

...other stuff

        @Override
        protected Void doInBackground(Void... arg0){
            HttpHandler sh = new HttpHandler();
            //faccio una richiesta al server e attendo risposta
            String jsonStr = sh.makeServiceCall(url); 
            Log.e("RAW-JSON: ","Retrieve RAW-Json is "+jsonStr);

            if (jsonStr != null) {
                try {
                    JSONObject jsonObj = new JSONObject(jsonStr);
                    String DATESTRING = jsonObj.getString("date");

                    JSONObject DATE = new JSONObject(DATESTRING); //Log.d("STAMPA","DATESTRING"+DATESTRING);

                    //Log.d("PER FAVORE","IERI ERA "+Ieri);
                    //ESTRAGGO E INSERISCO DOVE MI SERVE
                    String Yesterday = DATE.getString("ieri");
                    String Today = DATE.getString("oggi");
                    String Tomorrow = DATE.getString("domani");
                } catch (JSONException e) {
                    runOnUiThread(new Runnable(){
                        @Override
                        public void run(){
                            Toast.makeText(getContext(),"Errore nel recupero dati",Toast.LENGTH_LONG).show();
                        }
                    });
                    e.printStackTrace();
                }
            }

            return null;
        }
        @Override
        protected void onPostExecute(Void result){
            super.onPostExecute(result);
            //tolgo il caricamento di pDialog
            if (pDialog.isShowing())
                pDialog.dismiss();
        }
    }

    private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        adapter.addFragment(new YesterdayFragment(),Yesterday );  //i can set from here the name of tabs but how if the name from by Json?
        adapter.addFragment(new TodayFragment(), Today);
        adapter.addFragment(new TomorrowFragment(), Tomorrow);
        viewPager.setAdapter(adapter);
    }

1 个答案:

答案 0 :(得分:1)

您需要在Class Level Scope中声明您的变量。 在任何方法或代码块之外的类中定义

  String Yesterday , Today , Tomorrow = " ; //Define inside your class , outside of any method or code block,

        @Override
        protected Void doInBackground(Void... arg0){
            HttpHandler sh = new HttpHandler();
            //faccio una richiesta al server e attendo risposta
            String jsonStr = sh.makeServiceCall(url); 
            Log.e("RAW-JSON: ","Retrieve RAW-Json is "+jsonStr);

            if (jsonStr != null) {
                try {
                    JSONObject jsonObj = new JSONObject(jsonStr);
                    String DATESTRING = jsonObj.getString("date");

                    JSONObject DATE = new JSONObject(DATESTRING); //Log.d("STAMPA","DATESTRING"+DATESTRING);

                    //Log.d("PER FAVORE","IERI ERA "+Ieri);
                    //ESTRAGGO E INSERISCO DOVE MI SERVE
                    Yesterday = DATE.getString("ieri");
                    Today = DATE.getString("oggi");
                   Tomorrow = DATE.getString("domani");
                } catch (JSONException e) {
                    runOnUiThread(new Runnable(){
                        @Override
                        public void run(){
                            Toast.makeText(getContext(),"Errore nel recupero dati",Toast.LENGTH_LONG).show();
                        }
                    });
                    e.printStackTrace();
                }
            }

            return null;
        }
        @Override
        protected void onPostExecute(Void result){
            super.onPostExecute(result);
            //tolgo il caricamento di pDialog
            if (pDialog.isShowing())
                pDialog.dismiss();
        }
    }

    private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        adapter.addFragment(new YesterdayFragment(),Yesterday );  //i can set from here the name of tabs but how if the name from by Json?
        adapter.addFragment(new TodayFragment(), Today);
        adapter.addFragment(new TomorrowFragment(), Tomorrow);
        viewPager.setAdapter(adapter);
    }