Android, Java - How to use one DTO in two classes2019 Community Moderator ElectionHow do I pass data between Activities in Android application?Is Java “pass-by-reference” or “pass-by-value”?How do I efficiently iterate over each entry in a Java Map?How do I read / convert an InputStream into a String in Java?How do I generate random integers within a specific range in Java?Close/hide the Android Soft KeyboardWhy is the Android emulator so slow? How can we speed up the Android emulator?How do I convert a String to an int in Java?Creating a memory leak with JavaWhy is subtracting these two times (in 1927) giving a strange result?Proper use cases for Android UserManager.isUserAGoat()?

Can anyone tell me why this program fails?

Is it possible that AIC = BIC?

PTIJ: Who should pay for Uber rides: the child or the parent?

What options are left, if Britain cannot decide?

Make a transparent 448*448 image

Bastion server: use TCP forwarding VS placing private key on server

Is a lawful good "antagonist" effective?

Co-worker team leader wants to inject his friend's awful software into our development. What should I say to our common boss?

Can elves maintain concentration in a trance?

Happy pi day, everyone!

Old race car problem/puzzle

Why does Deadpool say "You're welcome, Canada," after shooting Ryan Reynolds in the end credits?

Replacing Windows 7 security updates with anti-virus?

Russian cases: A few examples, I'm really confused

How is the Swiss post e-voting system supposed to work, and how was it wrong?

Bash: What does "masking return values" mean?

How to deal with a cynical class?

Humanity loses the vast majority of its technology, information, and population in the year 2122. How long does it take to rebuild itself?

How do I hide Chekhov's Gun?

An Accountant Seeks the Help of a Mathematician

How could a scammer know the apps on my phone / iTunes account?

Why are the outputs of printf and std::cout different

What are the possible solutions of the given equation?

Why using two cd commands in bash script does not execute the second command



Android, Java - How to use one DTO in two classes



2019 Community Moderator ElectionHow do I pass data between Activities in Android application?Is Java “pass-by-reference” or “pass-by-value”?How do I efficiently iterate over each entry in a Java Map?How do I read / convert an InputStream into a String in Java?How do I generate random integers within a specific range in Java?Close/hide the Android Soft KeyboardWhy is the Android emulator so slow? How can we speed up the Android emulator?How do I convert a String to an int in Java?Creating a memory leak with JavaWhy is subtracting these two times (in 1927) giving a strange result?Proper use cases for Android UserManager.isUserAGoat()?










0















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<TMapPoint> 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










share|improve this question
























  • can we see your MainActivity

    – user3170251
    Mar 6 at 19:13











  • @user3170251 sorry for the late reply. I edited my post.

    – WonGyeong
    Mar 7 at 2:01












  • In your onclick(View v) listener, you're creating a new object, hence the reason why you're getting initial values. Instead, you would need to transfer the currentLocationDTO from your MainActivity to your MapListActivity. Here's a good guide on how to pass data between activities

    – user3170251
    Mar 7 at 14:07















0















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<TMapPoint> 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










share|improve this question
























  • can we see your MainActivity

    – user3170251
    Mar 6 at 19:13











  • @user3170251 sorry for the late reply. I edited my post.

    – WonGyeong
    Mar 7 at 2:01












  • In your onclick(View v) listener, you're creating a new object, hence the reason why you're getting initial values. Instead, you would need to transfer the currentLocationDTO from your MainActivity to your MapListActivity. Here's a good guide on how to pass data between activities

    – user3170251
    Mar 7 at 14:07













0












0








0








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<TMapPoint> 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










share|improve this question
















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<TMapPoint> 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







java android gps dto






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 7 at 2:00







WonGyeong

















asked Mar 6 at 18:52









WonGyeongWonGyeong

358




358












  • can we see your MainActivity

    – user3170251
    Mar 6 at 19:13











  • @user3170251 sorry for the late reply. I edited my post.

    – WonGyeong
    Mar 7 at 2:01












  • In your onclick(View v) listener, you're creating a new object, hence the reason why you're getting initial values. Instead, you would need to transfer the currentLocationDTO from your MainActivity to your MapListActivity. Here's a good guide on how to pass data between activities

    – user3170251
    Mar 7 at 14:07

















  • can we see your MainActivity

    – user3170251
    Mar 6 at 19:13











  • @user3170251 sorry for the late reply. I edited my post.

    – WonGyeong
    Mar 7 at 2:01












  • In your onclick(View v) listener, you're creating a new object, hence the reason why you're getting initial values. Instead, you would need to transfer the currentLocationDTO from your MainActivity to your MapListActivity. Here's a good guide on how to pass data between activities

    – user3170251
    Mar 7 at 14:07
















can we see your MainActivity

– user3170251
Mar 6 at 19:13





can we see your MainActivity

– user3170251
Mar 6 at 19:13













@user3170251 sorry for the late reply. I edited my post.

– WonGyeong
Mar 7 at 2:01






@user3170251 sorry for the late reply. I edited my post.

– WonGyeong
Mar 7 at 2:01














In your onclick(View v) listener, you're creating a new object, hence the reason why you're getting initial values. Instead, you would need to transfer the currentLocationDTO from your MainActivity to your MapListActivity. Here's a good guide on how to pass data between activities

– user3170251
Mar 7 at 14:07





In your onclick(View v) listener, you're creating a new object, hence the reason why you're getting initial values. Instead, you would need to transfer the currentLocationDTO from your MainActivity to your MapListActivity. Here's a good guide on how to pass data between activities

– user3170251
Mar 7 at 14:07












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55030266%2fandroid-java-how-to-use-one-dto-in-two-classes%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55030266%2fandroid-java-how-to-use-one-dto-in-two-classes%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Save data to MySQL database using ExtJS and PHP [closed]2019 Community Moderator ElectionHow can I prevent SQL injection in PHP?Which MySQL data type to use for storing boolean valuesPHP: Delete an element from an arrayHow do I connect to a MySQL Database in Python?Should I use the datetime or timestamp data type in MySQL?How to get a list of MySQL user accountsHow Do You Parse and Process HTML/XML in PHP?Reference — What does this symbol mean in PHP?How does PHP 'foreach' actually work?Why shouldn't I use mysql_* functions in PHP?

Compiling GNU Global with universal-ctags support Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Tags for Emacs: Relationship between etags, ebrowse, cscope, GNU Global and exuberant ctagsVim and Ctags tips and trickscscope or ctags why choose one over the other?scons and ctagsctags cannot open option file “.ctags”Adding tag scopes in universal-ctagsShould I use Universal-ctags?Universal ctags on WindowsHow do I install GNU Global with universal ctags support using Homebrew?Universal ctags with emacsHow to highlight ctags generated by Universal Ctags in Vim?

Add ONERROR event to image from jsp tldHow to add an image to a JPanel?Saving image from PHP URLHTML img scalingCheck if an image is loaded (no errors) with jQueryHow to force an <img> to take up width, even if the image is not loadedHow do I populate hidden form field with a value set in Spring ControllerStyling Raw elements Generated from JSP tagds with Jquery MobileLimit resizing of images with explicitly set width and height attributeserror TLD use in a jsp fileJsp tld files cannot be resolved