Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
940 views
in Technique[技术] by (71.8m points)

dynamic - View pager first and second fragment data is not updating when data changed in android?

i am using view pager (dynamic) for showing data from service. It is working fine but whenever i called the service again, the data in the view pager need to refresh and updating with new data (from service).

All the fragments in view pager are refreshing with new data but first two fragment of view pager are not updating with new data, those two fragments (first and second) are showing with previous data only. please any one help me.

my code:

RechargeplansActivity.java:

public class RechargeplansActivity  extends FragmentActivity /*implements RechargePlansFragment.UserInterface*/ {
    static final String TAG = RechargeplansActivity.class.getSimpleName();
    SectionsPagerAdapter mSectionsPagerAdapter;
    ViewPager mViewPager;
    Intent intent;
    private ArrayList<String> mPlanNamesList;
    private ArrayList<BrowsePlan> mBrowsePlansList;
    TextView tx_network;
    Dialog message_dialg;
    Context context;
    String operatorid,circleid;

    Map<String, ArrayList<BrowsePlan>> mBrowsePlansMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rechargeplans);

        context = this;

        intent = getIntent();
        setPlan_types(new ArrayList<String>());
        setBrowseplans(new ArrayList<BrowsePlan>());
        mBrowsePlansMap = new HashMap<String, ArrayList<BrowsePlan>>();
        //db = DatabaseHandler.getInstance(getApplicationContext());
        initUI();




        operatorid=intent.getStringExtra("op_id");
        circleid=intent.getStringExtra("cir_id");



        executeBrowsePlans();
        tx_network.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Browseplans_dialog();
            }
        });


    }

    /* It is used for initialization */
    private void initUI() {
        tx_network = (TextView) findViewById(R.id.tx_network);

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.pager);
        //iv_back = (ImageView) findViewById(R.id.iv_back);
        // Create the adapter that will return a fragment
        mSectionsPagerAdapter = new SectionsPagerAdapter(
                getSupportFragmentManager());

        mViewPager.setAdapter(mSectionsPagerAdapter);

    }


    /* Data getting from the service */
    private void executeBrowsePlans() {
        ServiceClient serviceClient = ServiceUtil.getServiceClient();



        mProgressDialog.show();
        serviceClient.getPlans_v2(operatorid, circleid, getshowPlansCallback);

    }



    /**
     * @return the al_browseplans
     */
    public ArrayList<BrowsePlan> getBrowseplans(String key) {
        if (key == null && !mPlanNamesList.contains(key)) {
            return new ArrayList<BrowsePlan>();
        }
        return mBrowsePlansMap.get(key);
    }

    /**
     * @return the al_browseplans
     */
    public ArrayList<BrowsePlan> getBrowseplans() {
        return mBrowsePlansList;
    }

    /**
     * @param al_browseplans
     *            the al_browseplans to set
     */
    public void setBrowseplans(ArrayList<BrowsePlan> al_browseplans) {
        this.mBrowsePlansList = al_browseplans;
    }

    protected void traceD(String msg) {

        // Log.d(TAG, msg);
    }

    /**
     * @return the plan_types
     */
    public ArrayList<String> getPlan_types() {
        return mPlanNamesList;
    }

    /**
     * @param plan_types
     *            the plan_types to set
     */
    public void setPlan_types(ArrayList<String> plan_types) {
        Log.v("Tag_plantype", "" + plan_types.size());

        this.mPlanNamesList = plan_types;
    }

    Callback<JsonObject> getshowPlansCallback = new Callback<JsonObject>() {

        @Override
        public void failure(RetrofitError arg0) {
            // TODO Auto-generated method stub
            dismissProgress();
        }

        @Override
        public void success(JsonObject resultUserInfo, Response arg1) {
            traceD(" Browse Plans" + resultUserInfo.toString());
            dismissProgress();



            // String
            // planTypes=resultUserInfo.get("Plantypes").getAsString();
            // mPlanNamesList.addAll(Arrays.asList(planTypes.split(",")));
            // mSectionsPagerAdapter.notifyDataSetChanged();
            if (resultUserInfo.has("Status")) {
                String Status = resultUserInfo.get("Status")
                        .getAsString();
                if(Status.equalsIgnoreCase("false")){
                    dismissProgress();
                }
                else if(Status.equalsIgnoreCase("true")) {

                    JsonArray array = resultUserInfo.get("Plans")
                            .getAsJsonArray();

                    Type type = new TypeToken<ArrayList<BrowsePlan>>() {
                    }.getType();
                    Gson gson = new Gson();

                    getBrowseplans().addAll(
                            (Collection<? extends BrowsePlan>) gson.fromJson(array,
                                    type));
                    AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {

                        @Override
                        protected Void doInBackground(Void... params) {
                            Log.v("TAG_PLANS_1",""+mBrowsePlansList.size());

                            if (mBrowsePlansList.size() > 0) {
                                Log.v("TAG_PLANS",""+mBrowsePlansList);


                                for (BrowsePlan bl : mBrowsePlansList) {
                                    if (mBrowsePlansMap.get(bl.getPlanType()) == null) {
                                        mBrowsePlansMap.put(bl.getPlanType(), new ArrayList<BrowsePlan>());
                                    }
                                    mBrowsePlansMap.get(bl.getPlanType()).add(bl);
                                    //setBrowseplans(mBrowsePlansMap.get(bl.getPlanType()));
                                    Log.v("TAG_PLANS_map", "" +  (bl.getPlanType().toString())
                                    );
                                }




                            }


                            // mBrowsePlansList.clear();
                            for (Map.Entry<String, ArrayList<BrowsePlan>> entry : mBrowsePlansMap.entrySet()) {

                                mPlanNamesList.add(entry.getKey());
                            }
                            return null;
                        }

                        protected void onPostExecute(Void result) {

                            for(BrowsePlan bp: getBrowseplans()){
                                System.out.println("plan type post execute: "+bp.getPlanType()+"value: "+bp.getPlanValue());
                            }

                           // mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

                          //  mViewPager.setAdapter(mSectionsPagerAdapter);

                            mSectionsPagerAdapter.notifyDataSetChanged();
                            //mViewPager.setCurrentItem(4);
                        }

                        ;

                    };
                    task.execute();
                }

            }

        }
    };

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();

        dismissProgress();
    }

   /* @Override
    public void setBrowsePlansToRefresh(RechargePlansFragment.BrowsePalnsAdapter adapter, ListView listView) {
        //adapter = new BrowsePlansAdapter();

    }

    @Override
    public void setData(ArrayList<BrowsePlan> plans) {
        mBrowsePlansList.clear();
        mBrowsePlansList = plans;
    }
*/

    /**
     * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
     * one of the sections/tabs/pages.
     */
    public class SectionsPagerAdapter extends FragmentPagerAdapter {
        private boolean doNotifyDataSetChangedOnce = false;

        public SectionsPagerAdapter(FragmentManager fragmentManager) {
            super(fragmentManager);
        }

        // Returns total number of pages
        @Override
        public int getCount() {
            if (doNotifyDataSetChangedOnce) {
                doNotifyDataSetChangedOnce = false;
                notifyDataSetChanged();
            }
            return getPlan_types().size();
        }

        // Returns the fragment to display for that page
        @Override
        public Fragment getItem(int position) {

            return RechargePlansFragment
                    .newInstance(mPlanNamesList.get(position));
        }

        // Returns the page title for the top indicator
        public CharSequence getPageTitle(int position) {
            return getPlan_types().get(position);
        }

        public int getItemPosition(Object object) {
            return POSITION_NONE;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {

            return super.instantiateItem(container, position);
        }

        @Override
        public long getItemId(int position) {
            return super.getItemId(position);
        }
    }

    @Override
    public void onActivityResult(int arg0, int arg1, Intent arg2) {

        setResult(arg1, arg2);
        traceD("onActivityResult ");
        finish();
        super.onActivityResult(arg0, arg1, arg2);
    }

    void showProgress(int msgID) {

        dismissProgress();
        if (mProgressDialog == null) {
            mProgressDialog = new ProgressDialog(this);
        }
        if (mProgressDialog != null && !mProgressDialog.isShowing()) {

            mProgressDialog.setMessage(getResources().getString(msgID));
            mProgressDialog.show();
        }

    }

    void dismissProgress() {

        if (mProgressDialog != null && mProgressDialog.isShowing()) {

            mProgressDialog.dismiss();
        }
    }
    @Override
    public void onResume() {
        super.onResume();
        mSectionsPagerAdapter.notifyDataSetChanged();
    }
    void Browseplans_dialog( ) {

        message_dialg = new Dialog(this);
        Window window = message_dialg.getWindow();
        message_dialg.requestWindowFeature(Window.FEATURE_NO_TITLE);
        message_dialg.getWindow().setBackgroundDrawable(new ColorDrawable(0));
        message_dialg.setContentView(R.layout.activity_selectdialog);
      Button  viewplans=(Button)message_dialg.findViewById(R.id.viewplans);
        viewplans.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                operatorid = "5";
                circleid="1";
                mPlanNamesList.clear();
                mBrowsePlansList.clear();
                mBrowsePlansMap.clear();



                executeBrowsePlans();
                message_dialg.dismiss();
            }
        });


        window.setLayout(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        window.setGravity(Gravity.CENTER);


        message_dialg.setCancelable(true);



        message_dialg.show();


    }

}

RechargePlansFragment .java:

public cl

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Just changed to FragmentStatePagerAdapter instead of FragmentPagerAdapter in SectionsPagerAdapter class

It is working now.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...