HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Location-based reminder application

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
basedreminderlocationapplication

Problem

I'm creating a location-based reminder application in Android using proximity alerts, geocoder, Google Maps API and their Places API.

Currently there is a default ListView using a Cardlibs library and a button for a new reminder, which then asks for the 'subject' and 'location'. There is a button for a map to allow the user to select a location from the map instead.

Then there is a "submit" button which sends a custom object called Reminder to the activity with the ListView.

The ListView Activity (MainActivity) is however separated into two fragments HomeFragment and WorkFragmen for respective 'profiles'. So far I haven't thought of a way to save the data, but I'm most likely heading to use SQLite.

Anyhow, this means that I'm questioning any use for this custom Reminder object if I'm just going to be inputting the metadata into a database anyway.

Is there a better way I could do this, and to quickly look over my code for the Reminder object and profiles (as I think the fragments could just extend a standard 'base' fragment as I'm duplicating code)?

Reminder:

```
public class Reminder implements Parcelable {

public double latitude;
public double longitude;
public String subject;
public String locationName;

public String profile;

public Reminder() {

}

public Reminder(Parcel in) {
String[] data = new String[5];

in.readStringArray(data);
this.subject = data[0];
this.locationName = data[1];
this.latitude = Double.parseDouble(data[2]);
this.longitude = Double.parseDouble(data[3]);
this.profile = data[4];
}

public String getProfile() {
return profile;
}

public double getLatitude() {
return latitude;
}

public String getLocationName() {
return locationName;
}

public double getLongitude() {
return longitude;
}

public String getSubject() {
return subject;
}

public vo

Solution

Reminder

-
there is no reason to have your Reminder fields latitude, longitude, .... as public fields, and there are good reasons to keep them private. You already have the getters and setters for them.

-
The Parcelable constructor should also be private. It will never be called from outside your class (unless you have tests, or something, but that can be solved in a different way).

-
In the Parcelable constructor you pre-define the size of the String array then call readStringArray(...). You should instead be using createStringArray().

The reality is that you should actually be doing the following:

@Override
public void writeToParcel(Parcel dest, int flags) {

    dest.writeString(this.subject);
    dest.writeString(this.locationName);
    dest.writeDouble(this.latitude);
    dest.writeDouble(this.longitude);
    dest.writeString(this.profile);
}


then, in the Parcel constructor you should have:

public Reminder(Parcel in) {
    this.subject = in.readString();
    this.locationName = in.readString();
    this.latitude = in.readDouble();
    this.longitude = in.readDouble();
    this.profile = in.readString();
}


Data Storage

The best way to store your data will depend on the system you choose to store it in.

Right now you are undecided. That's OK.

Your Reminder class is flexible enough for it not to matter right now. You can adapt at the time it happens.

Code Snippets

@Override
public void writeToParcel(Parcel dest, int flags) {

    dest.writeString(this.subject);
    dest.writeString(this.locationName);
    dest.writeDouble(this.latitude);
    dest.writeDouble(this.longitude);
    dest.writeString(this.profile);
}
public Reminder(Parcel in) {
    this.subject = in.readString();
    this.locationName = in.readString();
    this.latitude = in.readDouble();
    this.longitude = in.readDouble();
    this.profile = in.readString();
}

Context

StackExchange Code Review Q#44314, answer score: 3

Revisions (0)

No revisions yet.