android中dto的设计,Android, Java - How to use one DTO in two classes

I'm trying to make a map application.

There are two Activities that need to get current location.

The first activity is MainActivity, and all of the codes to get current location are in it.

And the second one is an activity that user can set a departure place.

When an user click a button to set a point of departure as current location, I need to get the values from the MainActivity.

I made CurrentLocationDTO to solve this. But somehow when I call currentDTO.getLatitude() in the second activity, it just returns 0.0.

I have no idea why it returns initialized value even if it's called after currentDTO.setLatitude(value); is all done in MainActivity.

I made the DTO object by writing CurrentLocationDTO currentDTO = new CurrentLocationDTO(); in each classes. Is it wrong to make it like this? Or is there any other ideas to solve this problem, please share with me.

here's MainActivity.java

public class MainActivity extends AppCompatActivity {

private final String TAG = this.getClass().getSimpleName();

private final String TMAP_API_KEY = "10062d5b-d3d3-4b7d-8977-acd3ac2dc390";

LocationManager lm;

TMapView tMapView;

EditText Departure, Arrival;

Button Search;

LinearLayout Layout;

FloatingActionButton Current, Navigate;

String D_lon;

String D_lat;

String A_lon;

String A_lat;

//floatingActionButton 활성화/비활성화

Boolean FabActive = false; //navigate 버튼용

private ArrayList arPoint;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

LinearLayout linearLayoutTmap = (LinearLayout) findViewById(R.id.linearLayoutTmap);

tMapView = new TMapView(this);

lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); //위치 관리자 객체 참조

Departure = findViewById(R.id.departure);

Arrival = findViewById(R.id.arrival);

Search = findViewById(R.id.search);

Layout = findViewById(R.id.search_layout);

Current = findViewById(R.id.current_location);

Navigate = findViewById(R.id.navigate);

tMapView.setSKTMapApiKey(TMAP_API_KEY);

linearLayoutTmap.addView(tMapView);

tMapView.setIconVisibility(true);

setGps();

Departure.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

startActivityForResult(new Intent(MainActivity.this, MapListActivity.class), 200);

}

});

Arrival.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

startActivityForResult(new Intent(MainActivity.this, MapListActivity.class), 300);

}

});

}

public void setGps() {

//final LocationManager lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&

ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_COARSE_LOCATION, android.Manifest.permission.ACCESS_FINE_LOCATION}, 1);

}

lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, // 등록할 위치제공자(실내에선 NETWORK_PROVIDER 권장)

1000, // 통지사이의 최소 시간간격 (miliSecond)

1, // 통지사이의 최소 변경거리 (m)

mLocationListener);

lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, // 등록할 위치제공자(실내에선 NETWORK_PROVIDER 권장)

1000, // 통지사이의 최소 시간간격 (miliSecond)

1, // 통지사이의 최소 변경거리 (m)

mLocationListener);

}

private LocationListener mLocationListener = new LocationListener() {

public void onLocationChanged(Location location) {

double latitude;

double longitude;

if (location != null) {

CurrentLocationDTO currentLocationDTO = new CurrentLocationDTO();

//I get values from location object here, and use setter to store it

latitude = location.getLatitude();

longitude = location.getLongitude();

currentLocationDTO.setLatitude(latitude);

currentLocationDTO.setLongitude(longitude);

Log.e("Test", currentLocationDTO.getLatitude() + ", " + currentLocationDTO.getLongitude()); //I can get proper values here

Log.e(TAG, latitude + " , " + longitude);

tMapView.setLocationPoint(longitude, latitude);

tMapView.setCenterPoint(longitude, latitude);

}

}

public void onProviderDisabled(String provider) {

}

public void onProviderEnabled(String provider) {

}

public void onStatusChanged(String provider, int status, Bundle extras) {

}

};

@Override

protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {

if (requestCode == 200) {

if (resultCode == RESULT_OK) {

String D_str = data.getStringExtra("selected");

D_lat = data.getStringExtra("lat");

D_lon = data.getStringExtra("lon");

Departure.setText(D_str);

// Toast.makeText(getApplicationContext(), "requestCode= " + requestCode, Toast.LENGTH_SHORT).show();

// Toast.makeText(this, D_str, Toast.LENGTH_SHORT).show();

Toast.makeText(this, "lat : " + D_lat + " lon : " + D_lon, Toast.LENGTH_SHORT).show();

}

}

if (requestCode == 300) {

if (resultCode == RESULT_OK) {

String A_str = data.getStringExtra("selected");

A_lat = data.getStringExtra("lat");

A_lon = data.getStringExtra("lon");

Arrival.setText(A_str);

// Toast.makeText(getApplicationContext(), "requestCode= " + requestCode, Toast.LENGTH_SHORT).show();

Toast.makeText(this, A_str, Toast.LENGTH_SHORT).show();

}

}

}

}

And This one is CurrentLocationDTO.java

package com.example.gpgpt.myapplication;

public class CurrentLocationDTO {

private double latitude;

private double longitude;

public double getLatitude() {

return latitude;

}

public void setLatitude(double latitude) {

this.latitude = latitude;

}

public double getLongitude() {

return longitude;

}

public void setLongitude(double longitude) {

this.longitude = longitude;

}

}

MapListActivity.java - I tried to use getter but it returns initial values of DTO

Current.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

CurrentLocationDTO currentLocationDTO = new CurrentLocationDTO();

double lon = currentLocationDTO.getLongitude();

double lat = currentLocationDTO.getLatitude();

currentLocationDTO.setLongitude(13.8);

Text.setText(lon+", "+lat);

}

});

Thank you

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值