java - 从 AlertDialog 按钮切换 Activity

标签 java android google-maps android-alertdialog

我正在开发一个基于 map 的应用程序。比方说,我有三个类: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,则无需点击 map 即可出现警报对话框。 我知道这很愚蠢,但由于我是 android 和 java 的新手,我希望你们能考虑一下。请帮忙 !

最佳答案

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

关于java - 从 AlertDialog 按钮切换 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12359827/

相关文章:

java - HttpClient执行时出现org.apache.http.client.ClientProtocolException

json - 澳大利亚房地产 API(用于 google maps mashup)

java - Spring Boot Rest API - 输入文件+端点

java - 寻找在字符串数组中搜索重复条目的优雅方法。蛮力法有效

android ScrollView 布局(wrap_content,最大尺寸)

javascript - 在调整窗口大小之前,无法让谷歌地图在 Bootstrap 模式中加载

javascript - 从谷歌地图中运行脚本?

java - 计算每个重叠间隔数的最佳 MapReduce 算法

java 公共(public) dbutils : multi-part identifier could not be bound

android - Activity的onDestroy/Fragment的onDestroyView设置Null做法