从AlertDialog按钮切换活动

时间:2012-09-10 21:34:44

标签: java android google-maps android-alertdialog

我正在研究基于地图的应用程序。比方说,我有三个类:MapsActivity,MyItemizedOverlay& GetDirectionActivity。在MyItemizedOverlay中,我想在单击肯定对话框按钮后切换到GetDirectionActivity。 ActiveDialog放在onTap方法下,这样我就可以得到GeoPoint。为此,我做了什么: 在ItemizedOverlay类中:

@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 ; 
            }

此处IDE显示我在startActivity(intent)行中有错误。 我也试过了: 在MyItemizedOverlay类中:

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

在MapsActivity类中:

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();
            } 

对于if语句我可以使用哪种条件?如果我将其设置为lat> 0,则会出现alertdialog而不会点击地图。 我知道这很愚蠢,但因为我是android和amp; java,我希望你们能考虑一下。请帮忙 !

1 个答案:

答案 0 :(得分:1)

在ItemizedOverlay类中的OnTap方法中放置:

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();