How to pass Latitude and Longitude from database to Google Maps Activity - java

I've set up a database which through a textview displays the latitude and longitude of a specific building. There is a "go" button underneath which loads up my mapsActivity. However, I can't seem to get the lat and long to be placed on the map.
Maps Activity
public class MapsActivity extends AppCompatActivity implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {
GoogleMap mGoogleMap;
SupportMapFragment mapFrag;
LocationRequest mLocationRequest;
GoogleApiClient mGoogleApiClient;
Location mLastLocation;
Marker mCurrLocationMarker, map1, map2, map3, map4, map5, map6, map7;
ZoomControls zoom;
// public static String TAG1;
// public static String TAG2;
public static String TAG_LAT = "lat";
public static String TAG_LNG = "lng";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
ToggleButton b1 = (ToggleButton) findViewById(R.id.button3);
getSupportActionBar().setTitle("Queen's Building Search");
Intent intent = getIntent();
mapFrag = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFrag.getMapAsync(this);
zoom = (ZoomControls) findViewById(R.id.zcZoom);
zoom.setOnZoomOutClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mGoogleMap.animateCamera(CameraUpdateFactory.zoomOut());
}
});
zoom.setOnZoomInClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mGoogleMap.animateCamera(CameraUpdateFactory.zoomIn());
}
});
Bundle extras = getIntent().getExtras();
if(extras != null){
double value = extras.getDouble("Latitude");
double value1 = extras.getDouble("Longitude");
// TAG_LAT = extras.getString("Latitude");
// TAG_LNG = extras.getString("Longitude");
goToLocation(value, value1);
}
Go to location Method being called
public void goToLocation(double lat, double lng) {
LatLng ll = new LatLng(lat, lng);
CameraUpdate update = CameraUpdateFactory.newLatLng(ll);
mGoogleMap.moveCamera(update);
// btnGo();
}
building activity
btnGoLocation.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Double lat = selectedBuilding.getLatitude();
Double lng = selectedBuilding.getLongitude();
Bundle bundle = new Bundle();
// Intent intent = new Intent(BuildingActivity.this, MapsActivity.class);
//Bundle extras = getIntent().getExtras();
Intent intent = new Intent(BuildingActivity.this, MapsActivity.class);
bundle.putDouble(MapsActivity.TAG_LNG,lat);
bundle.putDouble(MapsActivity.TAG_LNG, lng);
// Intent intent = new Intent(getApplicationContext(), MapsActivity.class);
// intent.putExtra("Latitude", +lat);
// intent.putExtra("Longitude", ""+lng);
// intent.putExtra("latitude", lat);
// intent.putExtra("longitude", lng);
intent.putExtras(bundle);
startActivity(intent);
// Double latt=extras.getDouble(lat);
//Double loNg=extras.getDouble(lng)
}
});
As you can see my code is pretty messy trying to get this to work. If anyone could teamview with me it'd be a lot easier to show what i'm trying to do. I feel like i'm close.

Did you try in your MapActivity:
intent.getDoubleExtra("Latitude", 0);
?
Edit: and by the way, it isn't a database... just intent between 2 activities. No persistence here.

Use this in BuildingActivity
Intent intent = new Intent(getApplicationContext(), MapsActivity.class);
intent.putExtra("Latitude", ""+lat);
intent.putExtra("Longitude", ""+lng);
In MapsActivity get this value in onCreate() callback using this way...
Intent intent = getIntent();
If your extra data is represented as strings, then you can use intent.getStringExtra(String name) method. In your case:
String lat = intent.getStringExtra("Latitude");
String lng = intent.getStringExtra("Longitude");
Now you have both lat and lng value in your MapsActivity.
Hope this will help you :)

Related

Save the LatLng values of Marker into your database

I'm new to coding and I'm currently stuck with how to save my map markers to my SQLite database. I have an existing database for my users and passwords. I'll attach my maps activity and database helper and my edit activity which creates the markers. Any help will be appreciated.
public class MapActivity2 extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
private static final int EDIT_REQUEST = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map2);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap map) {
this.mMap = map;
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
#Override
public void onMapClick(final LatLng latLng) {
Intent edit = new Intent(MapActivity2.this, EditActivity.class);
edit.putExtra("location", latLng);
MapActivity2.this.startActivityForResult(edit, EDIT_REQUEST);
}
});
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode) {
case (EDIT_REQUEST) : {
if (resultCode == Activity.RESULT_OK) {
MarkerOptions markerOptions = data.getParcelableExtra("marker");
mMap.addMarker(markerOptions);
}
break;
}
}
}}
This is the EditActivity which adds the marker to the map.
public class EditActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit);
final LatLng latlng = (LatLng) getIntent().getParcelableExtra("location");
final EditText title = (EditText) findViewById(R.id.title);
Button boton = (Button) findViewById(R.id.save);
boton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View view) {
MarkerOptions marker = new MarkerOptions().position(latlng);
if (title.getText() != null) {
marker.title(title.getText().toString());
}
Intent resultIntent = new Intent();
resultIntent.putExtra("marker", marker);
setResult(Activity.RESULT_OK, resultIntent);
finish();
}
});
}

How to pass an object to a new activity in android studio?

I am attempting to pass an object from one activity to another. I have tried using just intent and how with bundle but I am not sure what is wrong. I have looked at similar solutions here and that is where I got most of my code for this, but it seems that my copy and paste does not work.
This is my main class
public class MainActivity extends AppCompatActivity {
Item item;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createNewItem();
Button buttonOne = findViewById(R.id.itemButton);
buttonOne.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), ViewItemDetails.class);
Bundle bundle = new Bundle();
bundle.putSerializable("item", item);
intent.putExtras(bundle);
startActivity(intent);
}
});
}
void createNewItem(){
item=new Item("Pixel 4","https://google.com",1000.00);
}
}
This is the activity I am trying to go to:
public class ViewItemDetails extends AppCompatActivity {
Intent intent= getIntent();
Bundle bundle= intent.getExtras();
Item item = (Item) bundle.getSerializable("item");
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_item_details);
setStrings();
setButtons();
}
void setStrings() {
try {
TextView nameTextView = findViewById(R.id.itemNameid);
nameTextView.setText(item.getItemName());
TextView itemInitTV = findViewById(R.id.initalPriceNumID);
itemInitTV.setText(Double.toString(item.getInitPrice()));
TextView itemCurrTV = findViewById(R.id.currentPriceNumid);
itemCurrTV.setText(Double.toString(item.getCurrentPrice()));
}catch (NullPointerException e){
//do noting
}
}
void setButtons(){
Button buyButton = findViewById(R.id.buyButtonid);
buyButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Uri uriUrl = Uri.parse(item.getWebaddress());
Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uriUrl);
startActivity(launchBrowser);
}
});
Button refreshButton= findViewById(R.id.refrechId);
refreshButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Double newPrice= new GenerateNewPrice().GenerateNewPrice(item.getWebaddress());
Toast toast = Toast.makeText(getApplicationContext(),Double.toString(newPrice), Toast.LENGTH_SHORT);
toast.show();
}
});
Button editButton= findViewById(R.id.editid);
editButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast toast = Toast.makeText(getApplicationContext(),"Attempt to edit", Toast.LENGTH_SHORT);
toast.show();
}
});
}
}
This is the object I am attempting to pass between activities.
public class Item implements Serializable {
String itemName, webaddress;
Double initPrice, CurrentPrice;
public Item(String itemName, String webaddress, Double initPrice) {
this.itemName = itemName;
this.webaddress = webaddress;
this.initPrice = initPrice;
}
public String getItemName() {
return itemName;
}
public String getWebaddress() {
return webaddress;
}
public Double getInitPrice() {
return initPrice;
}
public Double getCurrentPrice() {
return CurrentPrice;
}
}
When I run the app on my phone I click the button and then the app closes.
Thank you for your help. If needed I can add more code. I have seen similar questions here, but they have not worked for me. I got similar code from those posts but have not solved my solution.
I appreciate any feedback that is give.
Thank you for your time.
For now and for future help on SOF, remember, Error logs are always helpful in that kind of scenario.
Though, Here are some points..
You should follow the Activity lifecycle rule, Getting the data in onCreate() will be a good idea.
You should use Parcelable instead of Serializable. It is much more efficient.
your initialisation of bundle and item is wrong in the ViewItemDetails.java activity. Try to initialise inside the onCreate method and let us know..
Seems like you have some bug, it should work, you can check out one of the project work example
//creating the budle
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//String selectedItem = (String) adapterView.getItemAtPosition(i);
//getting dat from text view and converting to string
// String textview =((TextView)view.findViewById(R.id.textViewName)).getText().toString();
//Toast.makeText(DriverInfo.this,"Driver id "+driver_id,Toast.LENGTH_SHORT).show();
Bundle bundle=new Bundle();
bundle.putString("DriverID",driver_id);
bundle.putString("Route",loc_src);
bundle.putString("Dest",loc_dest);
bundle.putString("location_address",location_address);
Toast.makeText(DriverInfo.this, "Driver ID="+driver_id, Toast.LENGTH_SHORT).show();
Intent intent=new Intent(DriverInfo.this, DirverCurrentLoc.class);
intent.putExtras(bundle);
startActivity(intent);
}
});
//Getting the bundle data
String lat, lng, realtime,src,dest;
location_address=intent.getStringExtra("location_address");
driver_id=intent.getStringExtra("DriverID");
src=intent.getStringExtra("Route");
dest=intent.getStringExtra("Dest");
You have to initialize this code in onCreate() method like below :
Item item;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_item_details);
Intent intent= getIntent();
Bundle bundle= intent.getExtras();
item = (Item) bundle.getSerializable("item");
setStrings();
setButtons();
}
You can use GSON to convert your Object into a JSON format
Intent intent = new Intent(this, ProjectActivity.class);
intent.putExtra("item", new Gson().toJson(item));
startActivity(intent);
then in your second activity, make sure you initialize the bundle correctly in onCreate() lifecycle.
Item item;
#Override
protected void onCreate(Bundle savedInstanceState) {
.........
Bundle extras = getIntent().getExtras();
if (extras != null) {
item = new Gson().fromJson(extras.getString("item"), Item.class);
}
Note: I read this in an article that google recommends GSON when passing objects on bundle.

makers are calling the same activity on google maps

I have a little problem with applying makers on Google maps to call activities.
I can successfully create the points and even call the activitys by clicking them, but the problem is that they are always calling the same activity no matter what I do.
Can someone help me?
Already tried to move the variables, already tried to move the celula01 variable to see makes a connection, but none of this gives any return.
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap, mCel;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mCel = googleMap;
LatLng celula04 = new LatLng(-23.174601, -45.839513);
mCel.addMarker(new MarkerOptions().position(celula04).title("Célula da Maria"));
mCel.moveCamera(CameraUpdateFactory.newLatLng(celula04));
mCel.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener(){
#Override
public boolean onMarkerClick(Marker marker) {
Intent intent = new Intent(MapsActivity.this, Celula02.class);
startActivity(intent);
return false;
}
});
LatLng celula01 = new LatLng(-23.173300, -45.821273);
mMap.addMarker(new MarkerOptions().position(celula01).title("Célula do Rafael"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(celula01));
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener(){
#Override
public boolean onMarkerClick(Marker marker) {
Intent intent = new Intent(MapsActivity.this, Celula01.class);
startActivity(intent);
return false;
}
});
}
}
Thanks.
public class MapsActivity extends FragmentActivity implements
OnMarkerClickListener,
OnMapReadyCallback {
private static final LatLng celula04 = new LatLng(-23.174601, -45.839513);
private static final LatLng celula01 = new LatLng(-23.173300, -45.821273);
private Marker mcelula04;
private Marker mcelula01;
private GoogleMap mMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.marker_demo);
SupportMapFragment mapFragment =
(SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
/** Called when the map is ready. */
#Override
public void onMapReady(GoogleMap map) {
mMap = map;
// Add some markers to the map, and add a data object to each marker.
mcelula04 = mMap.addMarker(new MarkerOptions()
.position(celula04)
.title("Célula da Maria");
mcelula04.setTag(1);
mcelula01 = mMap.addMarker(new MarkerOptions()
.position(celula01)
.title("Célula do Rafael");
mcelula01.setTag(2);
// Set a listener for marker click.
mMap.setOnMarkerClickListener(this);
}
/** Called when the user clicks a marker. */
#Override
public boolean onMarkerClick(final Marker marker) {
// Retrieve the data from the marker.
Integer click = (Integer) marker.getTag();
// Check if a click count was set, then display the click count.
if (click = 1) {
public boolean onMarkerClick(Marker marker) {
Intent intent = new Intent(MapsActivity.this, Celula02.class);
startActivity(intent);
}elseif(click = 2){
Intent intent = new Intent(MapsActivity.this, Celula01.class);
startActivity(intent);
}
// Return false to indicate that we have not consumed the event and that we wish
// for the default behavior to occur (which is for the camera to move such that the
// marker is centered and for the marker's info window to open, if it has one).
return false;
}
}

How to pass an edittext string from an activity to a method in another activity?

I have 2 EditTexts and I want to copy the String of these EditTexts and use them in a method in another activity.
I was making an app which types some details in two EditTexts and have a button which copies the String of these two EditTexts and paste or use it in a RecyclerView in another Activity.
I tried the Intent and Bundle medthods but could not solve it and actually it was hard to arrange the structure of codes.
This is the activity I want to pass from:
btn_save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (etTitle.length() != 0 || etDes.length() != 0){
addData();
}else {
Toast.makeText(DataInput.this, "Please Add Data !", Toast.LENGTH_SHORT).show();
}
}
});
}
private void addData() {
String titled = etTitle.getText().toString();
String desed = etDes.getText().toString();
Intent inte = new Intent();
Bundle bund = new Bundle();
bund.putString("title", titled);
bund.putString("des", desed);
inte.putExtras(bund);
startActivity(inte);
}
This is the activity I want to receive with:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(), DataInput.class);
startActivity(intent);
}
});
recyclerView = findViewById(R.id.rv);
dAdapter = new DataAdapter(dataList);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(layoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(dAdapter);
}
public void sendData() {
Bundle bundle = getIntent().getExtras();
String addedTitle = bundle.getString("title");
String addedDes = bundle.getString("dec");
Data data = new Data(addedTitle, addedDes);
dataList.add(data);
dAdapter.notifyDataSetChanged();
}
All I want is to pass the intent and bundle from addData method in the first Activity to the sendData method in the second Activity, so I can use the Strings to pass them in Data constructor.
Use bundle or intent.
// From activity1 to activity2
Intent intent = new Intent(Activity1.this, Activity2.class);
Bundle bundle = new Bundle();
bundle.putString(<key>, <value>);
intent.putExtras(bundle);
startActivity(intent);
// in activity 2 onCreate
Bundle bundle = intent.getExtras();
if (bundle != null) {
// get the value from bundle based on key
}
Here is the short example pass data from activity to activity
I would recommend you to change your implementation in following way so that we can avoid key mismatch issue accidently.
#Override
public void onClick(View v) {
if (etTitle.length() != 0 || etDes.length() != 0){
String title = etTitle.getText().toString();
String description = etDes.getText().toString();
Activity2.launch(this,title,description);
} else {
Toast.makeText(DataInput.this, "Please Add Data !", Toast.LENGTH_SHORT).show();
}
}
In calling activity you may create helper method say launch like below.
public static final String KEY_TITLE = "title";
public static final String KEY_DESCRIPTION = "description";
public static void launch(Context context, String title, String description) {
Intent intent = new Intent(context, Activity2.class);
Bundle data = new Bundle();
data.putString(KEY_TITLE, title);
data.putString(KEY_DESCRIPTION, description);
intent.putExtras(data);
context.startActivity(intent);
}
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = intent.getExtras();
if (bundle != null) {
String title = bundle.getString(KEY_TITLE);
String description = bundle.getString(KEY_DESCRIPTION);
}
}
To retrieve the text from an EditText you can use editText.getText().toString()
To retrieve the text from an EditText use
String value = editText.getText().toString();
And then pass the key value pair either through intent or bundle
Intent in = new Intent(Activity1.this, Activity2.class);
in.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
in.putExtra("key", value);
startActivity(in);
to receive put this in new activity
String string_name = getIntent().getExtras().getString("key");
Update:
There is key mismatch, you are sending key as "des" and receiving as "dec"

Filling a TextView with a Double from another class

I have an application that gets your current location. It will return your current address, latitude and longitude. When you click convert it takes you to a new class.
I want to get latitude and longitude (if it has something) into a textView in another class.
FIRST CLASS
public class MainActivity extends AppCompatActivity {
private static Button newWindowBtn; //new window button
Button btnShowLocation;
GPSTracker gps;
Double latitude;
Double longitude;
String addressStr;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
shareIt();
}
});
final TextView address = (TextView)findViewById(R.id.textView);
onClickButtonListener(); //NEW WINDOW CLICK LISTENER
btnShowLocation = (Button) findViewById(R.id.show_location); //GPS BUTTON
final Geocoder geocoder = new Geocoder(this,Locale.ENGLISH);
btnShowLocation.setOnClickListener(new View.OnClickListener()
{ //GPS CLICK LISTENER
#Override
public void onClick(View v) { //SHOWS LOCATION
gps = new GPSTracker(MainActivity.this);
if(gps.canGetLocation()){
latitude = gps.getLatitude();
longitude = gps.getLongitude();
try{
List<Address> addresses = geocoder.getFromLocation(latitude,longitude,1);
if(addresses != null)
{
Address returnedAddress = addresses.get(0);
StringBuilder strReturnedAddress = new StringBuilder("Address:\n");
for (int i = 0; i < returnedAddress.getMaxAddressLineIndex();i++){
strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("\n");
}
address.setText(strReturnedAddress.toString() + "Latitude: " + latitude + "\nLongitude: " + longitude);
addressStr = strReturnedAddress.toString();
}
else
{
address.setText("No address returned");
}
}
catch(IOException e){
e.printStackTrace();
address.setText("Can't get address");
}
}else{
gps.showSettingsAlert();
}
}
});
}
SECOND CLASS
public class secondActivity extends AppCompatActivity {
Button convertBtn;
TextView latitudeTxt;
TextView longitudeTxt;
TextView latitudeDMS;
TextView longitudeDMS;
convertTo convert = new convertTo();
MainActivity mainActivity = new MainActivity();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
shareIt();
}
});
convertBtn = (Button) findViewById(R.id.convertBtn);
latitudeTxt = (TextView)findViewById(R.id.latitudeTxt);
longitudeTxt = (TextView)findViewById(R.id.longitudeTxt);
latitudeDMS = (TextView)findViewById(R.id.latitudeDMS);
longitudeDMS = (TextView)findViewById(R.id.longitudeDMS);
convertBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
So I would want latitudeTxt to have the latitude from the previous screen (first class). I'm a student and trying to learn.
I've tried adding this to the first class:
secondActivity secondActivity = new secondActivity();
secondActivity.latitudeTxt.setText(latitude);
This didn't work.
I tried in the second class
MainActivity mainActivity= new MainActivity();
latitudeTxt.setText(mainActivity.latitude);
This approach is totally wrong and discouraged by Android.
MainActivity mainActivity= new MainActivity();
never EVER do that.
What you in fact are looking for is sharing data between activities, (there are a lot of questions like this in the community)
in the fisrt class (actually activity) use this as ref.:
static final int PICK_CONTACT_REQUEST = 1; // The request code
...
private void pickContact() {
Intent pickContactIntent = new Intent(Intent.ACTION_PICK, Uri.parse("content://contacts"));
pickContactIntent.setType(Phone.CONTENT_TYPE); // Show user only contacts w/ phone numbers
startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST);
}
and in the second:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Check which request we're responding to
if (requestCode == PICK_CONTACT_REQUEST) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
// The user picked a contact.
// The Intent's data Uri identifies which contact was selected.
// Do something with the contact here (bigger example below)
}
}
}
Send the latitude ,longitude values from 1st class to 2nd using bundle extras. Extract the value in 2nd class and set the values to the respective text view.
You should not create Objects of Activities like this,
MainActivity mainActivity= new MainActivity();
Activities are started by using Intents and if you need to refer to current object of your activity then you can do it by MainActivity.this.
And as far as, "This didn't work." is concerned, that is because you are creating a new object of your Activity class (which is correct syntax wise). And the new object will have its own copy of instance variables. So it will not be having values of already existing Activity object (which is shown to you).
To pass your values from say, FirstActivity to SecondActivity, do something like,
FirstActivity
Intent secondActivity = new Intent(FirstActivity.this, SecondActivity.class);
secondActivity.putExtra("latitude", value);
secondActivity.putExtra("longitude", value);
startActivity(secondActivity);
Then in SecondActivity
Intent intent = getIntent();
String latitude = intent.getStringExtra("latitude");
String longitude = intent.getStringExtra("longitude");

Categories