【问题标题】:Calling map activity multiple time from parent activity and store data automatically从父活动多次调用地图活动并自动存储数据
【发布时间】:2018-01-10 16:15:18
【问题描述】:

我有两个活动位置活动和家庭活动,并且有两个不同的按钮,两个启动位置活动 btnGPSStartPoint 和 btnGPSEndPoint。

我想在从 btnGPSStartPoint 启动位置活动时将 sendLocation 存储在 StartLatLong 变量中,在从 btnGPSEndPoint 启动位置活动时将 sendLocation 存储在 EndLatLong 变量中。

简而言之,我想使用活动作为方法,以 sendLocation 作为输出

请帮忙...

家庭活动:

        btnGPSStartPoint.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v) {
            startActivityForResult((new Intent(getApplicationContext(), Location.class)),1);
            StartLatLong = getLocation;

        }
    });


    btnGPSEndPoint.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v) {
            startActivityForResult((new Intent(getApplicationContext(), Location.class)),1);
            EndLatLong = getLocation;

        }
    });
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == 1) {
        if(resultCode == RESULT_OK){
            LatLng result= data.getParcelableExtra("sendLocation");
            getLocation = result;
        }
        if (resultCode == RESULT_CANCELED) {
            //Write your code if there's no result
        }

    }
}

位置活动:

FloatingActionButton SaveLocation = (FloatingActionButton) findViewById(R.id.location);
    SaveLocation.setOnClickListener(new View.OnClickListener() {
        @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
        @Override
        public void onClick(View view) {

            if (MarkerPosition == null) {
                Snackbar.make(view, "Please click on location first", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
            else {

                AlertDialog.Builder builder = new AlertDialog.Builder(Location.this);
                builder.setCancelable(true);
                builder.setTitle("Location");
                builder.setMessage("Latitude : " + MarkerPosition.latitude + "\n" + "Longitude : " + MarkerPosition.longitude);
                builder.setPositiveButton("Confirm",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Intent returnIntent = new Intent();
                                returnIntent.putExtra("sendLocation",MarkerPosition);
                                setResult(Activity.RESULT_OK,returnIntent);
                                finish();
                            }
                        });
                builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                });

                AlertDialog dialog = builder.create();
                dialog.show();
            }
        }
    });

【问题讨论】:

    标签: java android-studio-2.3


    【解决方案1】:

    终于解决了

    我使用不同的请求代码调用 Location 活动,并使用 if 语句捕获结果。

    但正在寻找一种将活动用作方法的方法

    家庭活动:

           btnGPSStartPoint.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v) {
                startActivityForResult((new Intent(getApplicationContext(), Location.class)),1);
            }
        });
    
    
        btnGPSEndPoint.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v) {
                startActivityForResult((new Intent(getApplicationContext(), Location.class)),2);
    
            }
        });
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    
        if (requestCode == 1) {
            if(resultCode == RESULT_OK){
                LatLng result= data.getParcelableExtra("sendLocation");
                StartLatLong = result;
            }
            if (resultCode == RESULT_CANCELED) {
                //Write your code if there's no result
            }
    
        }
    
        else if (requestCode==2){
                if(resultCode == RESULT_OK){
                    LatLng result= data.getParcelableExtra("sendLocation");
                    EndLatLong = result;
                }
                if (resultCode == RESULT_CANCELED) {
                    //Write your code if there's no result
                }
            }
    
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-17
      • 1970-01-01
      相关资源
      最近更新 更多