从活动中获取变量的值并将其传递给片段活动

时间:2017-07-24 05:59:52

标签: java android google-maps-markers

我想在我的Activity中获取mLocal的值,并将其传递给Fragment类的LatLng。

主要活动

    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);

            initLocationFetching(MainActivity.this);

        mapFragment = new MapFragment(MainActivity.this, this);
        manager = getSupportFragmentManager();
        manager.beginTransaction().replace(R.id.mainLayout, mapFragment).commit();
}
            @Override
        public void locationFetched(Location mLocal, Location oldLocation, String time, String locationProvider) {
        super.locationFetched(mLocal, oldLocation, time, locationProvider);
    > here mLocal
    }

地图片段类

 class MapFragment extends Fragment implements OnMapReadyCallback,
            GoogleApiClient.OnConnectionFailedListener {

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        LatLng origin = new LatLng(14.507328, 121.000905);

        mMap.addMarker(new MarkerOptions()
                .position(new LatLng(14.507577, 121.004456))
                .anchor(0.5f, 0.5f)
                .title("title")
                .snippet("snippet")
                .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_CYAN)));

        mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(value1, value2), 9.0f));

    }

上面,我希望它不是硬编码的。我想在MainActivity中获取mLocal的值并将其传递给LatLng()MapFragment。

6 个答案:

答案 0 :(得分:1)

你可以在活动中采用公开的全球性位置,并创建它的getter-setter

 

 MainActivity extends Activity

    public Location mLocal;
    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);

            @Override
        public void locationFetched(Location mLocal, Location oldLocation, String time, String locationProvider) {
        super.locationFetched(mLocal, oldLocation, time, locationProvider);
    setMLocal(mLocal);
    } 
     public String getMLocal() {
            return mLocal;
        }

        public void setMLocal(String mLocal) {
            this.mLocal= mLocal;
        }

MainActivity extends Activity public Location mLocal; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); @Override public void locationFetched(Location mLocal, Location oldLocation, String time, String locationProvider) { super.locationFetched(mLocal, oldLocation, time, locationProvider); setMLocal(mLocal); } public String getMLocal() { return mLocal; } public void setMLocal(String mLocal) { this.mLocal= mLocal; }

然后你可以在片段中使用getter方法

public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
    Location mLocal = ((MainActivity)getActivity).getMLocal(); //you can get your Location object and you can use it further
        //LatLng origin = new LatLng(14.507328, 121.000905);

        mMap.addMarker(new MarkerOptions()
                .position(new LatLng(14.507577, 121.004456))
                .anchor(0.5f, 0.5f)
                .title("title")
                .snippet("snippet")
                .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_CYAN)));

答案 1 :(得分:0)

您可以向MapFragment添加一个名为setLocation的方法,并在MainActivity中调用它。

像这样:

MapFragment

// Location mCurrentLoc = null;

public void setLocation(Location loc) {
    mCurrentLoc = loc;
    if (mMap != null) {
        //do fragment location stuff
    }
}

@Override
public void onMapReady(GoogleMap googleMap) {
    // all your above code

    if (mCurrentLocation != null) {
        // do fragment location stuff
    }
}

MainActivity

mapFragment.setLocation(mLocal);

答案 2 :(得分:0)

要将数据从活动传递到片段,您可以使用Bundle来放置您的变量并使用此Bundle设置片段的参数,或者您可以使用其他方式(我没有看到您的整个项目获得确定它是更好的方式)EventBus

答案 3 :(得分:0)

您的MapFragment实例是mapFragment 吗?

因此,在MapFragment.class中创建一个公共方法getCoordinates

    public void getCoordinates(double latitude, double longitude){
         this.latitude = latitude;
         this.longitude = longitude;
}

将坐标从Activity发送到片段,如下所示

if(mapFragment instanceOf MapFragment){
 ((MapFragment)mapFragment).getCoordinates(latitude, longitude);

答案 4 :(得分:0)

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        initLocationFetching(MainActivity.this);

    mapFragment = new MapFragment(MainActivity.this, this);
    manager = getSupportFragmentManager();
}
 @Override
    public void locationFetched(Location mLocal, Location oldLocation, String time, String locationProvider) {
    super.locationFetched(mLocal, oldLocation, time, locationProvider);
 //After initLocationFetching.
 Bundle bundle = new Bundle();
 bundle.putDouble("Lat",mLocal.getLatitude());
 bundle.putDouble("Long"mLocal.getLongitude());
 mapFragment.setArguments(bundle);
 manager.beginTransaction().replace(R.id.mainLayout, mapFragment).commit();
}



//In MapFragment
class MapFragment extends Fragment implements OnMapReadyCallback,
        GoogleApiClient.OnConnectionFailedListener {
double lati=0.0;
double longi=0.0;

@Override
public void onCreate(Bundle bundle){
lati=getArguments().getDouble("Lat");
longi=getArguments().getDouble("Long");
}

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    LatLng origin = new LatLng(lati,longi);

    mMap.addMarker(new MarkerOptions()
            .position(new LatLng(lati,longi))
            .anchor(0.5f, 0.5f)
            .title("title")
            .snippet("snippet")
            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_CYAN)));

    mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(value1, value2), 9.0f));

}

答案 5 :(得分:0)

您必须创建活动的实例,然后将其数据传递给片段

在您的片段中,声明MainActivity的变量以获取其上下文,从而能够在片段上对其进行操作,并声明要为其分配MainActivity中包含的值的变量,然后传递该变量 onAttach 方法上的值。 onAttch在片段首次附加到其上下文时被调用。

private MainActivity mActivity;
private String mData;

现在在 onAttach 方法中,即片段粘附到其容器上,这是主要活动。

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    //Verify if mActivity is null and it is MainActivity context
    if (mActivity == null && context instanceof MainActivity) {
        mActivity = (MainActivity) getActivity();
        //Checking for a NPE       
        if(mActivity != null){
         mData = mActivity.MyMainActivityVariable;
       }
     }
   }

并通过 onDetach 方法 当片段不再与其活动关联时调用。

@Override
public void onDetach() {
    super.onDetach();
    mActivity = null;
    }

现在,我们可以在MainActivity中使用我们要使用的变量。 这是使用支持库片段 有关片段的更多信息,请访问Fragment Android Developers