简体   繁体   中英

Switch activity from a AlertDialog button

I'm working on a map based application . Let's say , I have three classes : MapsActivity , MyItemizedOverlay & GetDirectionActivity . In MyItemizedOverlay , I want to switch to GetDirectionActivity after the positive dialog button is clicked . ActiveDialog is placed under onTap method , so that I can get the GeoPoint. For this , what I've done : In ItemizedOverlay class :

@Override
public boolean onTap(GeoPoint p, MapView mapView) {
    // TODO Auto-generated method stub
    int lat = p.getLatitudeE6();
    int lot = p.getLongitudeE6();
    AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
    dialog.setTitle("Confirmation");
    dialog.setMessage("Confirm this as end point ?");
    dialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int arg1) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(mContext, GetDestination.class);
            startActivity(intent);
        }
    });
    dialog.setNegativeButton("No", null);
    dialog.show();
    return true ; 
            }

here IDE shows that I have a error in startActivity(intent) line . I've tried that also : In MyItemizedOverlay class :

@Override
public boolean onTap(GeoPoint p, MapView mapView) {
            return super.onTap(p, mapView); }

In MapsActivity class :

GeoPoint point2 = null ;
            confirmationOverlay.onTap(point2, mapView) ;
            int latt = point.getLatitudeE6() ;
            int longt = point.getLongitudeE6();
            final int endpointArray [] = {latt , longt};
            if(some condition to show the alert dialog after tapping)
            {
                AlertDialog.Builder dialog = new AlertDialog.Builder(MapsActivity.this);
                dialog.setTitle("Confirmation");
                dialog.setMessage("Confirm this location as end point ?");
                dialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int arg1) {
                        // TODO Auto-generated method stub
                        Intent intent = new Intent(MapsActivity.this,GetDestination.class);
                        intent.putExtra("geopoint" , endpointArray);
                        startActivity(intent);
                    }
                });
                dialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int arg1) {

                    }
                });
                dialog.show();
            } 

For the if statement what sort of condition I can use ? If I set it just like lat>0 then the alertdialog appears without tapping on the map. I know this is very silly , but since I am new in both android & java , I hope you guys will consider it. Please help !

On your OnTap method in ItemizedOverlay class put:

AlertDialog dialog = new AlertDialog.Builder(MapsActivity.context).create();
           dialog.setTitle("Confirmation");
           dialog.setMessage("Confirm this location as end point ?");

     dialog.setButton(DialogInterface.BUTTON_POSITIVE,"Yes", new DialogInterface.OnClickListener() {
         public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub

     Intent intent = new Intent(MapsActivity.context, GetDestination.class);
                        //you must put your map activity    
                            MapsActivity.context.startActivity(intent);


                        }   
                    });


              dialog.show();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM