如何在同一活动android

时间:2019-03-28 00:03:12

标签: android

我是Android新手,我尝试将2个双精度变量从一种方法传递到另一种方法,以将其上载到Firebase数据库,但是我没有成功,这些变量来自放置在地图上的位置。如何将数据从searchLocation传递到SaveDatasMap并将其上传到数据库?

我试图将它们放置在方法中的静态位置,但不允许我放置它。我尝试将双精度变量放在开头,但是我无法让它们具有要求它们提供的值:在所选变量中包含经度和纬度

public  void searchLocation(View view) {


    EditText locationSearch = (EditText) findViewById(R.id.editText);
    final String location = locationSearch.getText().toString();
    List<Address> addressList = null;

    if (location != null || !location.equals("")) {
        Geocoder geocoder = new Geocoder(this);
        try {
            addressList = geocoder.getFromLocationName(location, 1);

        } catch (IOException e) {
            e.printStackTrace();
        }
        final Address address = addressList.get(0);
        final LatLng latLng = new LatLng(address.getLatitude(), address.getLongitude());
        mMap.clear();
        mMap.addMarker(new MarkerOptions().position(latLng).title(location));
        mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
        Toast.makeText(getApplicationContext(),address.getLatitude()+" "+address.getLongitude(),Toast.LENGTH_LONG).show();
 double       longitud = address.getLatitude();
   double     latitud = address.getLatitude();










    }


}

public void  SaveDatasMap (View view)
{
    GuardarUbicacion = (Button) findViewById(R.id.guardarubicacion);
    final DatabaseReference RootRef;
    RootRef = FirebaseDatabase.getInstance().getReference();
    RootRef.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

            if (!(dataSnapshot.child("Empresa").child(currentUserID).exists()))
            {
                HashMap<String, Object> userdataMap = new HashMap<>();
                userdataMap.put("Longitud", longitud);
                userdataMap.put("Longitud", latitud);

                RootRef.child("Empresa").child(currentUserID).updateChildren(userdataMap)
                        .addOnCompleteListener(new OnCompleteListener<Void>() {
                            @Override
                            public void onComplete(@NonNull Task<Void> task) {

                                if(task.isSuccessful())
                                {
                                    Toast.makeText(MapTemporalActivity.this, "Se ha guardado la posicion exitosamente", Toast.LENGTH_SHORT).show();

                                    Intent intent = new  Intent(MapTemporalActivity.this, LoginActivity.class);
                                    startActivity(intent);


                                }
                                else
                                {

                                    Toast.makeText(MapTemporalActivity.this, "Hubo un  error", Toast.LENGTH_SHORT).show();
                                }
                            }
                        });

            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });
}

}

我需要传递变量经度和纬度。 我将数据上传到数据库,但是我不知道如何传递searchLocation的双精度变量或直接将address.getLatitude()和address.getLongitude()传递给方法SavesDataMap

我了解我必须在静态方式下执行该方法,但不允许我使用android studio

1 个答案:

答案 0 :(得分:2)

您可以将这两个变量设为全局变量。像这样实例化它们。 double longitud; double latitud;

在您的searchLocation方法中执行以下操作: longitud = address.getLatitude(); latitud = address.getLatitude();

然后,您应该可以在SaveDatasMap方法中获取数据。