4
\$\begingroup\$

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 void setProfile(String profile) {
        this.profile = profile;
    }

    public void setLatitude(double latitude) {
        this.latitude = latitude;
    }

    public void setLocationName(String locationName) {
        this.locationName = locationName;
    }

    public void setLongitude(double longitude) {
        this.longitude = longitude;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    @Override
    public int describeContents() {
        return 0;
    }

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

        dest.writeStringArray(new String[] { this.subject, this.locationName,
        String.valueOf(this.latitude), String.valueOf(this.longitude), this.profile });
    }

    public static final Parcelable.Creator<Reminder> CREATOR = new Parcelable.Creator<Reminder>() {

        public Reminder createFromParcel(Parcel source) {
            // TODO Auto-generated method stub
            return new Reminder(source); // using parcelable constructor
        }

        public Reminder[] newArray(int size) {
            // TODO Auto-generated method stub
            return new Reminder[size];
        }
    };
}

List using a fragment:

public class HomeFragment extends Fragment {

    public static ArrayList<Card> cards = new ArrayList<Card>();

    Reminder reminder;

    public HomeFragment() {
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);

        reminder = getActivity().getIntent().getParcelableExtra("reminder");

        Card card = new Card(getActivity());

        CardHeader cardHeader = new CardHeader(getActivity());

        if (reminder != null) {
            cardHeader.setTitle(reminder.getSubject());
            card.addCardHeader(cardHeader);
            card.setTitle(reminder.getLocationName());
            cards.add(card);
        }

        CardArrayAdapter mCardArrayAdapter = new CardArrayAdapter(getActivity(), cards);

        CardListView listView = (CardListView) getActivity().findViewById(R.id.card_list);
        mCardArrayAdapter.notifyDataSetChanged();
        if (listView != null)
            listView.setAdapter(mCardArrayAdapter);
        }
\$\endgroup\$
3
\$\begingroup\$

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.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ I didn't see createStringArray - Thanks. But currently whenever the application is destroyed, the data is removed - The proximity alerts seem to stay but I'm hoping to save the data so they can then further edit each reminder if need be. \$\endgroup\$ – abstrakt Mar 14 '14 at 4:49
  • \$\begingroup\$ @abstrakt I am not suggesting that you don't need to store the data. I am suggesting that the best way to store the data depends on where you want to store it.... and, until you decide, I can't help. \$\endgroup\$ – rolfl Mar 14 '14 at 4:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.