如何在活动之间传递参数?

时间:2018-07-31 12:34:54

标签: android listview android-intent

我在一个活动中有一个列表项,在另一个活动中有一个mapView。我想做的是,当我单击第一个listItem时,它将显示当前位置以及我添加到其中的特殊位置,单击其他listItems之后,它还将显示当前位置,但在相同条件下将显示不同的特殊位置

List<Hero> heroList;
ListView listView;

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

heroList = new ArrayList<>();

heroList.add(new Hero(R.drawable.food,"Restaurant", "Justice League"));
heroList.add(new Hero(R.drawable.captainamerica,"Shopping Mall", "Avengers"));
heroList.add(new Hero(R.drawable.doctorstrange,"Hospital", "Avengers"));
heroList.add(new Hero(R.drawable.ironman,"Pharmacy", "Avengers"));
heroList.add(new Hero(R.drawable.joker,"Library", "Injustice League"));
heroList.add(new Hero(R.drawable.spi,"Second Hand", "Avengers"));

listView = (ListView) findViewById(R.id.listview1);

final MyCustomAdapter adapter = new MyCustomAdapter(this,R.layout.list_item,heroList);

listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
        Intent pos = new Intent(getApplicationContext(),MapsActivity2.class);
            pos.putExtra("asdas",position);
            startActivity(pos);
        if (position==0){
            Intent myIntent = new Intent(view.getContext(),MapsActivity2.class);
            startActivityForResult(myIntent,0);
        }
        if (position==1){
            Intent myIntent = new Intent(view.getContext(),MapsActivity2.class);
            startActivityForResult(myIntent,1);
        }
        if (position==2){
            Intent myIntent = new Intent(view.getContext(),MapsActivity2.class);
            startActivityForResult(myIntent,2);
        }
        if (position==3){
            Intent myIntent = new Intent(view.getContext(),MapsActivity2.class);
            startActivityForResult(myIntent,3);
        }
        if (position==4){
            Intent myIntent = new Intent(view.getContext(),MapsActivity2.class);
            startActivityForResult(myIntent,4);
        }
        if (position==5){
            Intent myIntent = new Intent(view.getContext(),MapsActivity2.class);
            startActivityForResult(myIntent,5);
        }
    }
});






}

}

这是我的listView

GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener, com.google.android.gms.location.LocationListener{

private static final int MY_PERMISSION_CODE = 1000;
private GoogleMap mMap;
private GoogleApiClient mGoogleApiClient;

private double latitude,longitude;
private Location mLastLocation;
private Marker mMarker;
private LocationRequest mLocationRequest;

IGoogleAPIService mService;
private static final LatLng BaniaLuca = new LatLng(50.064146, 19.935940);
private static final LatLng Cocon = new LatLng(50.047654,19.947358);
private  Marker mBaniaLuca;
private Marker mCocon;
int asdas ;


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

    KrakowActivity asd = new KrakowActivity();
    Bundle extras = getIntent().getExtras();

    if(extras != null){
        int asdas = extras.getInt("asdas");
    }

    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

    mService = Common.getGoogleAPIService();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
    {
        checkLocationPermission();
    }
}

private boolean checkLocationPermission()
{
    if (ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)
    {
        if (ActivityCompat.shouldShowRequestPermissionRationale(this,Manifest.permission.ACCESS_FINE_LOCATION))
            ActivityCompat.requestPermissions(this,new String[]{

                    Manifest.permission.ACCESS_FINE_LOCATION

            },MY_PERMISSION_CODE);
        else
            ActivityCompat.requestPermissions(this,new String[]{

                    Manifest.permission.ACCESS_FINE_LOCATION

            },MY_PERMISSION_CODE);
        return false;
    }
    else
        return true;
}


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

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
    {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED)
        {
            buildGoogleApiClien();
            mMap.setMyLocationEnabled(true);
        }
        else
        {
            buildGoogleApiClien();
            mMap.setMyLocationEnabled(true);
        }
    }


}

private synchronized void buildGoogleApiClien()
{
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();
    mGoogleApiClient.connect();
}

@Override
public void onConnected(@Nullable Bundle bundle) {
    mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(1000);
    mLocationRequest.setFastestInterval(1000);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
    if (ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED)
    {
        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,mLocationRequest,this);
    }
}

@Override
public void onConnectionSuspended(int i) {
    mGoogleApiClient.connect();
}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

}

@Override
public void onLocationChanged(Location location) {
    mLastLocation = location;
    if (mMarker != null)

        mMarker.remove();

    latitude = location.getLatitude();
    longitude = location.getLongitude();

    LatLng latLng = new LatLng(latitude,longitude);
    MarkerOptions markerOptions = new MarkerOptions()
            .position(latLng)
            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED));
    mMarker = mMap.addMarker(markerOptions);



    mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
    mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng,13));
  // if (asdas == 0) {
       mBaniaLuca = mMap.addMarker(new MarkerOptions()
               .position(BaniaLuca)
               .title("BaniaLuca"));
       mBaniaLuca.setTag(0);

       mCocon = mMap.addMarker(new MarkerOptions()
               .position(Cocon)
               .title("Cocon"));
       mCocon.setTag(0);

 //  }else if (asdas == 1){

    /*   mBaniaLuca = mMap.addMarker(new MarkerOptions()
               .position(BaniaLuca)
               .title("asdsad"));
       mBaniaLuca.setTag(0);

       mCocon = mMap.addMarker(new MarkerOptions()
               .position(Cocon)
               .title("asdsadasdas"));
       mCocon.setTag(0);*/

      /* AlertDialog asd = new  AlertDialog.Builder(this).create();
       asd.setMessage("asdasdsadas");
       asd.show(); */
 //  }else if (asdas == 2){


      /* AlertDialog asd = new  AlertDialog.Builder(this).create();
       asd.setMessage("asdasdsadas");
       asd.show(); */
  // }


    if (mGoogleApiClient != null)
        LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient,this);
}




}

这是我的MapView。基本上,我想发送(位置)到另一个活动,这样它就会理解它已单击并更改了地图方向。在此先感谢您的帮助,我是一个初学者,请您原谅我的错误。

1 个答案:

答案 0 :(得分:0)

您必须使用Intent的Extra部分。除了传递列表索引外,您还可以直接传递所需的坐标。它假定坐标是链接到列表项的数据的一部分。