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

Posted

技术标签:

【中文标题】如何在活动之间传递参数?【英文标题】:How to pass arguments between Activities? 【发布时间】:2019-01-07 20:37:06 【问题描述】:

我在一个活动中有一个列表项,在另一个活动中有一个 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);
        
    
);









这是我的列表视图

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。基本上,我想将(位置)发送到另一个活动,这样它就会明白它点击并更改了地图方向。在此先感谢您的帮助,我是初学者,请原谅我的错误。

【问题讨论】:

到底是什么问题?这是非常基本的,有 许多 示例通过此 只是谷歌。 我知道这是超级基本的。我想要的是在单击不同的 listItems 后,它们将显示相同的活动,但条件不同。 【参考方案1】:

您必须使用 Intent 的 Extra 部分。您可以直接传递您想要的坐标,而不是传递列表索引。它假设坐标是链接到列表项的数据的一部分。

【讨论】:

我明白了,非常感谢先生!!

以上是关于如何在活动之间传递参数?的主要内容,如果未能解决你的问题,请参考以下文章

在活动之间传递参数 - null

如何在页面之间传递 GCLID

如何将某些活动的上下文作为参数传递

安卓学习,参数的双向传递

如何使用安全参数将数据从导航抽屉传递到活动?

重新创建一个活动并传递参数