What Im trying to achieve is display points on a map using postcode. I found a script that does this for one postcode, but I would like to display a few at once. So I was thinking of changing the code around a little so the map was created in the first class.
My question is, how do I transfer the values of Class B to Class A?
2 values 'lng' and 'lat' need to be an array
My full code is here if that makes more sense http://pastebin.com/L6tcuPW9
Any help would be great
Class A extents FragmentActivity
{
}
This class gets retrieves the lng and lat values
Class B
{
public static double[] lat;
public static double[] lng;
public void retrievePost( String post)
{
// does a search and retrieves lng and lat
setLat(lat);
setLng(lng);
}
public static void setLat(double lat2)
{
// How do I get this value back into Class A
}
public static void setLng(double lng2)
{
// How do I get this value back into Class A
}
}
Try this:
public interface MyInterface {
public void setLat(double lat);
public void setLng(double lng);
}
class A extends FragmentActivity implements MyInterface{
#Overwrite
public void setLat(double lat) {
//Do Something
}
#Overwrite
public void setLng(double lng){
//Do Something
}
}
Class B
{
private MyInterface interface;
public B(MyInterface interface){
this.interface = interface;
}
public void retrievePost( String post){
interface.setLat(lat);
interface.setLng(lng);
}
}
Related
I tried to save this object using Realm But I got this error
Error:(24, 9) error: Type
java.util.ArrayList of field
savedPath is not supported
Here is my code :
public class TrackingInfo extends RealmObject {
private int order_id;
private double savedDistance;
private double savedDuration;
private ArrayList <LatLng>savedPath;
public int getOrder_id() {
return order_id;
}
public void setOrder_id(int order_id) {
this.order_id = order_id;
}
public double getSavedDistance() {
return savedDistance;
}
public void setSavedDistance(double savedDistance) {
this.savedDistance = savedDistance;
}
public double getSavedDuration() {
return savedDuration;
}
public void setSavedDuration(double savedDuration) {
this.savedDuration = savedDuration;
}
public ArrayList<LatLng> getSavedPath() {
return savedPath;
}
public void setSavedPath(ArrayList<LatLng> savedPath) {
this.savedPath = savedPath;
}
public TrackingInfo(){}}
Thanx in advance
Both List and LatLng cannot be stored in Realm directly. You will need to create a model object for LatLng and then use a RealmList object containing your model objects.
public class Location extends RealmObject {
public Location() { }
double latitude;
double longitude;
}
RealmList<Location> savedPath = new RealmList<Location>();
//Add location objects to savedPath and store it in your TrackingInfo object
You will need to manually convert objects of the LatLng class to the Location class when you are inserting/retrieving from the database.
I have a Linked List in one activity (A) that I want to share with another Activity (B).
The list contains a username of type string and contains coordinates of type LatLng. I am also using Intent and bundle to share data between activities. I tried using Parcelable but unable to figure out how to use it. Here is the code I have:
data.java
public class data implements Parcelable{
private LatLng coordinates;
private String name;
public data() {
name = null;
coordinates = null;
}
public data(String name, LatLng coordinates)
{
this.name = name;
this.coordinates = coordinates;
}
public data(Parcel in) {
coordinates = in.readParcelable(LatLng.class.getClassLoader());
name = in.readString();
}
public static final Creator<data> CREATOR = new Creator<data>() {
#Override
public data createFromParcel(Parcel in) {
return new data(in);
}
#Override
public data[] newArray(int size) {
return new data[size];
}
};
public LatLng getLatLng () {
return coordinates;
}
#Override
public int describeContents() {
return hashCode();
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeParcelable(coordinates, flags);
}
}
Activity A
public class A extends FragmentActivity implements
OnMapReadyCallback,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
GoogleMap.OnMyLocationButtonClickListener,
ActivityCompat.OnRequestPermissionsResultCallback {
Button switchToSeek;
double mLatitude;
double mLongitude;
LinkedList<data> storedData = new LinkedList<>();
protected void onCreate(Bundle savedInstanceState) {
...
switchToSeek.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getCurrentLocation();
Intent intent = new Intent(A.this, B.class);
Bundle xy = new Bundle();
xy.putDouble("x", mLatitude);
xy.putDouble("y", mLongitude);
xy.putParcelable("list", storedData); <---------- error: wrong second arugment
intent.putExtra("xy", xy);
A.this.startActivity(intent);
}
});
Activity B
public class B extends FragmentActivity implements OnMapReadyCallback {
double mLatitude;
double mLongitude;
LatLng current;
GoogleMap gMap;
LinkedList <data> copyData = new LinkedList<>();
#Override
public void onMapReady(GoogleMap googleMap) {
gMap = googleMap;
...
Intent intent = getIntent();
Bundle xy = intent.getBundleExtra("xy");
if (xy != null) {
mLatitude = xy.getDouble("x");
mLongitude = xy.getDouble("y");
}
/***** Call linked list here and set equal to copyData *****/
current = new LatLng(mLatitude, mLongitude);
gMap.moveCamera(CameraUpdateFactory.newLatLngZoom(current, 18.0f));
}
There is no easy way to do that, since LinkedList does not implement serializable or parcelable.
You CAN implement your own linked list class and make it a serializable/parcelable object which can then be passed.
Or you can convert its content into another data type such as an array and then recreate the linkedlist.* THIS IS HIGHLY INEFFICIENT
I believe there are other ways but this is a standard problem in android dev. Maybe try using fragments if possible and passing the linkedlist through a setter()
If the list is not huge, you can do it using the following helper class:
public class ParcelableLinkedList<E extends Parcelable> implements Parcelable {
private final LinkedList<E> linkedList;
public final Creator<ParcelableLinkedList> CREATOR = new Creator<ParcelableLinkedList>() {
#Override
public ParcelableLinkedList createFromParcel(Parcel in) {
return new ParcelableLinkedList(in);
}
#Override
public ParcelableLinkedList[] newArray(int size) {
return new ParcelableLinkedList[size];
}
};
public ParcelableLinkedList(Parcel in) {
// Read size of list
int size = in.readInt();
// Read the list
linkedList = new LinkedList<E>();
for (int i = 0; i < size; i++) {
linkedList.add((E)in.readParcelable(ParcelableLinkedList.class.getClassLoader()));
}
}
public ParcelableLinkedList(LinkedList<E> linkedList) {
this.linkedList = linkedList;
}
LinkedList<E> getLinkedList() {
return linkedList;
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel parcel, int flags) {
// Write size of the list
parcel.writeInt(linkedList.size());
// Write the list
for (E entry : linkedList) {
parcel.writeParcelable(entry, flags);
}
}
}
In your onClick() method, add the data to the Bundle like this:
xy.putParcelable("list", new ParcelableLinkedList<data>(storedData));
To extract the data from the Bundle, do this:
copyData = ((ParcelableLinkedList<data>)xy.getParcelable("list")).getLinkedList();
I haven't actually compiled and tested this code, but it should work.
If the list is really huge, you are better off storing it in a static member variable in one class and then just referencing it from the other. This isn't normally the way you want to do things in Android, but it is sometimes more expedient to do this than to serialize and deserialize a huge amount of data just to pass it between 2 activities that have access to the same memory space.
I have a model like this one:
class MyItem implements ClusterItem {
private LatLng mPosition;
private String mTitle;
public MyItem(){
}
#Override
public LatLng getPosition() {
return mPosition;
}
.
.
.
}
I am able to push the items to Firebase, but whenever I try to retrieve them I get the following exception:
com.google.firebase.database.DatabaseException: Class com.google.android.gms.maps.model.LatLng is missing a constructor with no arguments.
Does anybody have an idea how to fix this? Does it mean that my Firebase model cannot implement ClusterItem?
Good way to do is just create a java class which will store the lat long with an empty constructor and you can retrieve the same modal class. Something like this
public class MyLatLng {
private double lat;
private double lng;
public MyLatLng() {}
public MyLatLng(double lat, double lng) {
this.lat = lat;
this.lng = lng;
}
}
But if you're intent on using a DataSnapshot, you can override FirebaseRecyclerAdapter.parseSnapshot to handle the "UR Object":
I am making a game. There is a MainActivity with several layout over the mainscreen. One of the layout involves Google user login / logout / leaderboard / achievement.
The code are as follows:
MainActivity.class
public class MainActivity extends Activity
{
public static MainActivity main;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
main=this;
setContentView(R.layout.activity_main);
GameAdapter.StartGame();
....
}
The GameAdapter which then invoke MainLayout to inflates the different layout over the main screen.
MainLayout.class
public class MainLayout
{
public static double FloorY,Gravity,WalkSpeed;
public static void Inicial()
{
....
SoundAdapter.init();
FrontLayout.init();
....
}
FrontLayout.class (extending BaseGameActivity so as for Google Game Services)
public class FrontLayout extends BaseGameActivity
{
public static Urect LoginHolder;
public static Uimage Btn_lb, Btn_achieve, Btn_login, Btn_logout;
public static Uimage Info, info_intro;
public static void init()
{
Holder = Urect.CreateHolder(true);
LoginHolder=new Urect(0, 0, Screen.Width/2, Screen.Width/2);
Holder.AddChild(LoginHolder);
Btn_login =new Uimage(0, 0, Screen.Width/6.5, Screen.Width/6.5, Media.btn_login);
Btn_logout =new Uimage(0, 0, Screen.Width/6.5, Screen.Width/6.5, Media.btn_logout);
LoginHolder.AddChild(Btn_login);
LoginHolder.AddChild(Btn_logout);
Btn_login.addOnClickDownListner(new ClickDownListner()
{
#Override
public void OnClickDownDo(Urect curentRect, double X, double Y)
{
beginUserInitiatedSignIn(); ###1
}
});
Btn_logout.addOnClickDownListner(new ClickDownListner()
{
#Override
public void OnClickDownDo(Urect curentRect, double X, double Y)
{
signOut(); ###2
Btn_login.setAlpha(0); // visible
Btn_logout.setAlpha(255); //gone
}
});
Btn_lb.addOnClickDownListner(new ClickDownListner()
{
#Override
public void OnClickDownDo(Urect curentRect, double X, double Y)
{
if(isSignedIn()) ###3
{
MainActivity.main.startActivityForResult(Games.Leaderboards.getLeaderboardIntent(getApiClient(), MainActivity.main.getString(R.string.leaderboard_highest_mark)), 2); ###4
}
else
{
Utilities.custom_toast(MainActivity.main, "Not yet signed in", "gone", "short", "center");
}
}
});
Btn_achieve.addOnClickDownListner(new ClickDownListner()
{
#Override
public void OnClickDownDo(Urect curentRect, double X, double Y)
{
if(isSignedIn()) ###3A
{
MainActivity.main.startActivityForResult(Games.Achievements.getAchievementsIntent(getApiClient()), 1); ###4A
}
else
{
Utilities.custom_toast(MainActivity.main, "Not yet signed in", "gone", "short", "center");
}
}
});
}
#Override
public void onSignInFailed()
{
Btn_login.setAlpha(0); // visible
Btn_logout.setAlpha(255); // gone
}
#Override
public void onSignInSucceeded()
{
Btn_login.setAlpha(255); // gone
Btn_logout.setAlpha(0); // visible
}
}
Questions:
Referring to the locations marked in the above code:
###1: Cannot make a static reference to the non-static method beginUserInitiatedSignIn() from the type BaseGameActivity
###2: Cannot make a static reference to the non-static method signOut() from the type BaseGameActivity
###3, 3A: Cannot make a static reference to the non-static method isSignedIn() from the type BaseGameActivity
###4, 4A: Cannot make a static reference to the non-static method getApiClient() from the type BaseGameActivity
The error marked are basically the raised from the same error...but how could such be solved? Many thanks in advance!
I wrote a code to draw a path from gpx file. If the user turns the smartphone, the path is cleared, then I created a ArrayList<MyLatLng> where every MyLatLng object is:
public class MyLatLng implements Parcelable {
private double latitude;
private double longitude;
public MyLatLng(double lat, double lon) {
latitude = lat;
longitude = lon;
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel parcel, int arg) {
parcel.writeDouble(latitude);
parcel.writeDouble(longitude);
}
public static final Parcelable.Creator<MyLatLng> CREATOR = new Creator<MyLatLng>() {
#Override
public MyLatLng createFromParcel(Parcel parcel) {
double latitude = parcel.readDouble();
double longitude = parcel.readInt();
return new MyLatLng(latitude, longitude);
}
#Override
public MyLatLng[] newArray(int size) {
return new MyLatLng[size];
}
};
//Metodi get/set
public double getLatitude() {
return latitude;
}
public double getLongitude() {
return longitude;
}
}
And using onSaveInstanceState I avoid the cancellation of the path. But this method introduces a non-elegance in my code because every time I need to create a LatLng object from MyLatLng object. Do you have any advice about this? Thanks a lot :)