【问题标题】:Communication between two Activities?两个Activity之间的通信?
【发布时间】:2018-06-05 05:48:26
【问题描述】:

我正在制作一个使用 GoogleMaps 的应用程序。现在,我有一个关于 MapActivity 和 ToursActivity 之间通信的问题。为了简化它,我的应用程序是关于金属乐队的,我有 Tours 列表。当用户单击其中一个 Tours 时,它应该打开一个具有该位置的新 Activity。我不会把 ToursActivity 放在这里,因为有一堆你不需要的代码。另外,如果这很重要,我会将所有数据保存在 Firebase 上。

这是我的地图活动:

public class MapActivity extends AppCompatActivity implements OnMapReadyCallback {



private static final int REQUEST_LOCATION_PERMISSION = 10;
private GoogleMap.OnMapClickListener mCustomOnMapClickListener;

private GoogleMap mGoogleMap;
private MapFragment mMapFragment;
@BindView(R.id.lvTours) ListView lvTours;


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

    lvTours.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        }
    });
}


public void initialize(){
    this.mMapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.fGoogleMap);
    this.mMapFragment.getMapAsync(this);
    this.mCustomOnMapClickListener = new GoogleMap.OnMapClickListener() {
        @Override
        public void onMapClick(LatLng latLng) {
            MarkerOptions newMarkerOptions = new MarkerOptions();
            newMarkerOptions.icon(BitmapDescriptorFactory.fromResource(R.mipmap.tour));
            newMarkerOptions.title("Tour");
            newMarkerOptions.snippet("It' was here!");
            newMarkerOptions.position(latLng);
            mGoogleMap.addMarker(newMarkerOptions);
        }
    };
}


@Override
public void onMapReady(GoogleMap googleMap) {
    this.mGoogleMap = googleMap;
    UiSettings uiSettings = this.mGoogleMap.getUiSettings();
    uiSettings.setZoomControlsEnabled(true);
    uiSettings.setMyLocationButtonEnabled(true);
    uiSettings.setZoomGesturesEnabled(true);
    this.mGoogleMap.setOnMapClickListener(this.mCustomOnMapClickListener);
   goToLocation(33.835293 , -117.914505);
}


public void goToLocation(double lat, double lng){

    LatLng latLng = new LatLng(lat, lng);


    CameraPosition position = CameraPosition.builder()
            .target(latLng)
            .zoom(16f)
            .bearing(0.0f)
            .tilt(0.0f)
            .build();

    mGoogleMap.animateCamera(CameraUpdateFactory.newCameraPosition(position),null);
}




private boolean hasLocationPermission() {

    String LocationPermission = android.Manifest.permission.ACCESS_FINE_LOCATION;
    int status = ContextCompat.checkSelfPermission(this, LocationPermission);
    if (status == PackageManager.PERMISSION_GRANTED) {

        this.mGoogleMap.setMyLocationEnabled(true);

        return true;

    }
    return false;
}

private void requestPermission() {
    String[] permission = new String[]{Manifest.permission.ACCESS_FINE_LOCATION};
    ActivityCompat.requestPermissions(MapActivity.this, permission, REQUEST_LOCATION_PERMISSION);

}
}

【问题讨论】:

标签: android android-intent android-maps


【解决方案1】:

您可以使用带有额外参数的 Intent。 例如,在 ToursActivity.java 上

Intent intent = new Intent(ToursActivity.this, MapActivity.class);
intent.putExtra("lat", latitude);
intent.putExtra("long", longitude);
startActivity(intent);

然后你可以在 MapActivity.java 的 onCreate() 方法中获取这些参数:

Intent intent = getIntent();
long latitude = intent.getLongExtra("lat", 0);
long longitutde = intent.getLongExtra("long", 0);

【讨论】:

  • 感谢您的快速回答。
【解决方案2】:

您可以使用带有额外参数的意图。

Intent intent = new Intent(ToursActivity.this, MapActivity.class);
intent.putExtra("latitude", latitude);
intent.putExtra("longitude", longitude);
startActivity(intent);

然后你可以在 MapActivity 的 onCreate() 方法中获取你的值。

Intent intent = getIntent();
Bundle extras = intent.getExtras();
double latitude = intent.getDouble("latitude");
double longitude = intent.getDouble("longitude");

【讨论】:

  • 感谢您的快速回答,但我还有一个问题。当我写参数纬度和经度时,它们会变红,因为这些参数来自 MapActivity。现在,在我的 ToursFragment(不是 ToursActivity,我的错误)中是一个 onItemClick 方法,我想要 Intent,当你点击它时,它应该用地图打开一个新的活动
  • 你能把 onItemClick 代码放上来让我明白你的意思吗?
  • @OnItemClick(R.id.lvTours) public void onItemClick(View v,int position) { Intent intent = new Intent(v.getContext(),MapActivity.class); intent.putExtra("纬度", lat); intent.putExtra("经度", lng );开始活动(意图); } }
  • 我不知道我是否正确理解了您的问题,但这里是。在 MapActivity 中,当您获得意图时,您可以随意命名参数。如果这不是您问题的答案,请澄清您所说的“红色”是什么意思以及发生的位置。
  • 我会尽力解释得更好。我有 Firebase 上的 Tours 列表。该列表包含下一个参数:坐标、标题、id 和年份。我知道如何在我的 Fragment 中显示这些数据等等。但是,我做了你所说的(使用 Intent),问题是当我输入我的参数(ToursFragment.this,MapActivitiy)时,这两个得到红色下划线,它说:无法解析构造函数。现在,另一个问题出在第二行代码中。当我输入经度时,它变红了。
猜你喜欢
  • 1970-01-01
  • 2014-01-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多