1

My MainActivity has a double latitude value that I would like to pass to a fragment inside a FragmentPagerAdapter, so I created this getter method in MainActivity.java:

public double getLatitude() {
   return this.latitude;
}

Now I am trying to pass this value to a fragment when I create it (the getItem() method is part of a FragmentPagerAdapter class):

    public Fragment getItem(int position) {
            if (position == 0) {
                return new Fragment1();
            } else if (position == 1) {
                return new Fragment2();
            } else {
                Fragment3 fragment3 = new Fragment3();
                Bundle args = new Bundle();
                args.putDouble(fragment3.ARG_PARAM1, getActivity().getLatitude());
                fragment3.setArguments(args);
                return fragment3;
            }
        }

However, I get a compilation error that says Error:(156, 68) error: cannot find symbol method getActivity().

How can I get my main activity's getLatitude() when I create a new instance of a fragment?

user1301428
  • 1,743
  • 3
  • 25
  • 57

3 Answers3

1

Why don't you pass that value in Constructor of Adapter like

suppose your adapter class name is ViewPagerPagerAdapter then do following in that adapter

public class ViewPagerPagerAdapter extends FragmentPagerAdapter {

private double mLattitude;
public ViewPagerPagerAdapter (double lattitude) {
    this.mLattitude = lattitude;
}

//Now use that mLattitude wherever you want

public Fragment getItem(int position) {
            if (position == 0) {
                return new Fragment1();
            } else if (position == 1) {
                return new Fragment2();
            } else {
                Fragment3 fragment3 = new Fragment3();
                Bundle args = new Bundle();
                args.putDouble(fragment3.ARG_PARAM1, mLattitude);
                Log.d("WHATEVER", "WHATEVER: " + String.valueOf(mLattitude));
                fragment3.setArguments(args);
                return fragment3;
            }
        }
}

Now when you initiate the adapter from MainActivity , do

ViewPagerAdapter adapter = new ViewPagerAdapter(lattitude);
Sandip Fichadiya
  • 3,430
  • 2
  • 21
  • 47
  • This will work if latitude does not change. If it does, then you will have to add a setter method and update your adapter from activity every time latitude changes.. – Gennadii Saprykin Mar 10 '16 at 16:43
  • well then we can cast the context we pass in Constructor to activity & access whatever we want, like ((MainActivity)context).getLatitude() – Sandip Fichadiya Mar 10 '16 at 16:47
  • Yes, I agree. I think that will work better. Also, if you use an interface then you will not need to cast. E.g. when you create an adapter that is used in many activities you don't have to check the instance of activity and cast every time. – Gennadii Saprykin Mar 10 '16 at 16:50
  • So what would be the best way to update the latitude of a fractal when the location changes? – user1301428 Mar 11 '16 at 07:53
1

FragmentPagerAdapter is not a Fragment, i.e. it does not have getActivity() method. If you need to access a latitude from your activity just pass an activity reference to your FragmentPagerAdapter constructor. Using an interface here, something like LatitudeProvider would be even more flexible.

Example:

class MainActivity extends Activity implements LatitudeProvider {

    // pager adapter creation
    new MyFragmentPagerAdapter(this);

    @Override
    public double getLatitude() {
        return this.latitude;
    }
}

Your fragment pager adapter:

interface LatitudeProvider {
    double getLatitude();
}

public class MyFragmentManagerAdapter extends FragmentPagerAdapter {
    private final LatitudeProvider latitudeProvider;

    public MyFragmentManagerAdapter(LatitudeProvider latitudeProvider) {
        this.latitudeProvider = latitudeProvider;
    }

    ...
    // Now you can get your latitude like this:
    latitudeProvider.getLatitude();
}
Gennadii Saprykin
  • 4,505
  • 8
  • 31
  • 41
  • Should the declaration of the interface go within MainActivity as well? – user1301428 Mar 11 '16 at 07:51
  • No, it should be in the same file with adapter, or, in a separate file, whatever you like. It doesn't make much sense to declare it in MainActivity because it doesn't belong to activity. Interface describes something that provides a latitude value, it can be anything. So, don't create this extra dependency. Maybe you'll decide to move latitude logic into a separate class later on or maybe you will have multiple activities that provide latitude. Having latitude provider outside MainActivity makes more sense to me. – Gennadii Saprykin Mar 11 '16 at 16:24
-1

getActivity() returns context not object of MainActivity. As far as i know, There is no way to get current object of Activity in android. so you should make it public and static as following.

public static double getLatitude() {
   return this.latitude;     // make latitude public and static as well 
}

Now in your MainActivity.java

MainActivity.getLatitude();

if value is not changing. Then you can pass it in constructor.

vabhi vab
  • 419
  • 4
  • 11