1

I am trying to update the data passed to my adapter and hence update Fragments whose contents depends on these data,

here is the adpater:

import android.app.Fragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.support.v13.app.FragmentPagerAdapter;
public class ScheduleFragment_Pager_adapter extends FragmentPagerAdapter {

        private String CurrentSelectedYearID="",CurrentSelectedTermID="";

        public ScheduleFragment_Pager_adapter(FragmentManager fm , String SelectedYearID , String SelectedTermID) {
            super(fm);
            CurrentSelectedYearID = SelectedYearID;
            CurrentSelectedTermID = SelectedTermID;
        }

        @Override
        public Fragment getItem(int position) {

            Bundle bundle = new Bundle();
            bundle.putString("CurrentSelectedYearID", CurrentSelectedYearID);
            bundle.putString("CurrentSelectedTermID",CurrentSelectedTermID);

            if(position==0)
            {
                ClassesFragment classesFragment = new ClassesFragment();
                classesFragment.setArguments(bundle);
                return classesFragment;
            }
            else if(position==1)
            {
                ExamsFragment examsFragment = new ExamsFragment();
                examsFragment.setArguments(bundle);
                return examsFragment;
            }

            return null;
        }

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

        @Override
        public int getCount() {
            return 2;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0:
                    return "Classes";
                case 1:
                    return "Exams";
            }
            return null;
        }
    }

I have tried to use this answer, but It did not work , or I couldn't understand it !

and here how I use the adapter and how I try to update it.

at onCreateView of a Main Fragment [I am working with nested Fragment]

  mViewPager.setAdapter(new ScheduleFragment_Pager_adapter(getChildFragmentManager(), mSelectedYearID, mSelectedTermID));

and I update the adapter like this

@Override    
public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode == 0){
            if(resultCode == Activity.RESULT_OK){
                mSelectedTermID = data.getStringExtra("mSelectedTermID");
                mSelectedYearID = data.getStringExtra("mSelectedYearID");
                mViewPager.getAdapter().notifyDataSetChanged();
            }
        }
    }

Where, the onActivityResult is where mSelectedTermID and mSelectedYearID are being updated.

any Help?

Community
  • 1
  • 1
Atef Hares
  • 4,715
  • 3
  • 29
  • 61
  • How do you pass mSelectedTermID and mSelectedYearID to your adapter? I see you pass them to constructor, but later to update adapter how do you deliver them? – Lina Shyshova May 06 '16 at 11:00
  • @temna_nich, `mSelectedTermID` , `mSelectedYearID ` are global variables at before I call `setAdapter` at `onCreateView` I give them values, this makes the adapter works fine, then these values could be changed at `onActivityResult` and I did not pass them again-- should i pass them again ? ,,, Is that what are u asking for ? – Atef Hares May 06 '16 at 11:05
  • Add log message or breakpoint and check `mViewPager.getAdapter().notifyDataSetChanged();` was called. – Maxim G May 06 '16 at 11:08
  • Sure you should try it. As you pass them to constructor and this is the way they come into your adapter. But I do not see how do you pass them after update. Try to use a setter method. For this you need to store Adapter as variable in your fragment and update values in it. – Lina Shyshova May 06 '16 at 11:09
  • @temna_nich, how to pass them again to constructor, I am using `mViewPager.getAdapter()` – Atef Hares May 06 '16 at 11:13
  • @MaximG, I putted a break points at `onActivityResult` and this line is being executed : `mViewPager.getAdapter().notifyDataSetChanged();` is that what you mean? – Atef Hares May 06 '16 at 11:14
  • @temna_nich, yes I used a different way to pass these data again to the adpater and it worked, thank you so much, please put an answer so I can accept it – Atef Hares May 06 '16 at 11:18

3 Answers3

1

Try to use FragmentStatePagerAdapter

Explained here

Community
  • 1
  • 1
Lina Shyshova
  • 546
  • 5
  • 12
1

make adapter class variable in fragment

private ScheduleFragment_Pager_adapter adapter;

onCreateView of a Main Fragment

adapter = new ScheduleFragment_Pager_adapter(getChildFragmentManager(), mSelectedYearID, mSelectedTermID)
 mViewPager.setAdapter(adapter);

in your adapter

public void setValues( String SelectedYearID , String SelectedTermID) {
        CurrentSelectedYearID = SelectedYearID;
        CurrentSelectedTermID = SelectedTermID;
    }

onActivityResult

  adapter.setValues(mSelectedTermID, mSelectedYearID);
  adapter.notifyDataSetChanged();
Lina Shyshova
  • 546
  • 5
  • 12
  • Of Course, u r write, that is exactly what i did using ur advice of passing the data again. Thank you so Much – Atef Hares May 06 '16 at 11:21
  • yes, I used `FragmentStatePagerAdapter` Instead of `FragmentPagerAdapter` and Overridden `getItemPosition` with always returning `POSITION_NONE` and passed the data again to the adapater and it worked – Atef Hares May 06 '16 at 11:25
  • but is there any other efficient way to override my `getItemPosition` – Atef Hares May 06 '16 at 11:26
  • Yes, I got to your point , but Could u give me a little help with the code of `getItemPosition` . at setting/updating adapter I did this: `Object [] Frags={new ClassesFragment(), new ExamsFragment()};` and passed it to adapter – Atef Hares May 06 '16 at 11:42
  • Why should you even override getItemPosition? I checked my adapters and I never override this method, could you try to remove it? – Lina Shyshova May 06 '16 at 11:47
  • So let's look on the adapter code http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.0.1_r1/android/support/v4/view/PagerAdapter.java#PagerAdapter.getItemPosition%28java.lang.Object%29 – Lina Shyshova May 06 '16 at 11:52
  • As I understand by passing POSITION_NONE you force adapter to redraw your fragment every time. So maybe you should leave it as it is. – Lina Shyshova May 06 '16 at 11:54
  • Yes, Thats true and that why it is not efficient – Atef Hares May 06 '16 at 11:56
  • Have you tried this http://stackoverflow.com/a/17544134/1727937, maybe this will allow you do not override getItemPosition? – Lina Shyshova May 06 '16 at 12:00
0

FragmentPagerAdapter stores all fragments of each page the user visits.

By returning POSITION_NONE and every call notifyDataSetChanged() adapter should drop cached fragment and re-create a new.

Did you check notifyDataSetChanged() was called?

Be aware by returning POSITION_NONE you loose cache mechanism and for huge views, multiple pages it can be an issue.

You can implement observer pattern - register created fragments, and call them back when update is needed.

Maxim G
  • 1,479
  • 1
  • 15
  • 23
  • `FragmentStatePagerAdapter` worked fine for me, however I have implemented `getItemPosition` to always `POSITION_NONE` but is there another efficient way to implement it ? – Atef Hares May 06 '16 at 11:32
  • Observer pattern [example](https://github.com/mgolokhov/dodroid/blob/36ca89506657f744945f16838fc4ecab65827b8f/app/src/main/java/doit/study/droid/QuestionsPagerAdapter.java) with FragmentStatePagerAdapter but the idea is same. – Maxim G May 06 '16 at 11:38
  • This is some how advanced to me – Atef Hares May 06 '16 at 11:57