I'm really new to databases, but I'm making a running tracker app in Android, and I have some classes that represent the type of goals that you can have on a single type of workout.
What every Goal class does actually is to store a value but together with a unit, like for example a DistanceGoal will represent its value in meters and provide methods to convert it to kilometers or miles. A PaceGoal value will represent minutes per kilometers, and so on.
Also, according to type of Goal I will then have to read all tracking stats to see if the user has achieved the objective or not.
So:
interface GoalĀ {
long getGoalValue();
}
class TimeGoal implements Goal{}
class DistanceGoal implements Goal{}
class PaceGoal implements Goal{}
/* ...... */
Now this works well, although there may be a better way to solve this. The problem is that I need to store this in a database.
I have been using the Android room library, and I couldn't find any other way than to store each type of goal in its own table, so when I need to retrieve them I know what stored value represents what kind of goal.
But when I have to store a new goal, it will probably be given as its abstraction (the Goal interface), so I don't know in which table to store it.
The only solution I thought of is the Visitor Pattern, do any of you have a better solution for this?
I may have to make changes to the whole class hierarchy, but what really matters is to distinguish the unit from one value from the others, in order to read the tracking statistics accordingly.
You could make the Goal classes serializable. Then you can serialize the Goal objects to a byte array that you can store in your database. When you retrieve the byte array from the database, you can deserialize it to get the Goal object back. When you Google serializable java you should get lots of information on how to do this.
Related
If I have a class containing of integers, strings and a Date, how should I go about creating, saving and displaying a new instance of this class in my ListViews?
I've used SharedPreferences before for saving local strings but is this possible with objects as well, or should I look into SQLlite?
Edit: Also, if SQLlite is needed, a little steer in the right direction would be appreciated.
You can still use preferences to save your objects but you need to serialize them into strings. And for later use you have to deserialize them from strings into your list of objects. This can easily be done with Json.net but I would recommend to use a database like SQLite. This would make it easy if new requirements like searching or object extension comes up.
A good entry into SQLite and Xamarin can be found here or here.
I have a LongSparseArray variable, in which the objects stored implement the interface Comparable.
Is there a easy way to sort them, without do it "manually"?
I tried Collections.sort(myLongSparseArray), but it does not implements the List interface.
Another way could be convert it to a List, but still I have not found any method to do that.
SparseArray, or LongSparseArray, should be considered as an efficient hash table when the keys are integers or longs. As such, it is not the best class to use if ordering is important to you.
Usually, when using hash-table type data structures, then uniqueness of values & efficiency of get / set are important.
If this is the case, perhaps you should look into using LinkedHashSet? It provides a way of holding unique items (based on their hashCode & equals functions), but also preserves the order of items, and has high efficiency of get / set.
If sorting is important, then you could extract the value list from the LinkedHashSet, then place it in a List, and use Collections.Sort() on it.
I'm new in programming for Android so maybe my question will be very easy to solve but still. I'm trying to make an array of different data types :
I have to add there :
int number
String name
int number_2
int time
int total
And my question now is how to implement it in easiest way, and how to get data from it. In case that I have to get a different records for this variables and store it into list .
Also have a question about way how to keep all values which I handle inside of my array.
I have to keep it because in my program I have to go back to other activities go forward to another and again collect data and add it to my array.
What will be the best and easiest solution ?
Thanks in advance for help
You could create the Array as an Array of Objects. All other classes are derived from Object, so you'll be able to store all types of objects in your Array. However, you would have to check the type of an object you get from the Array, before you'd be able to safely interpret as an object of a specific class. Moreover, you would have to use Integer instead of int.
If all (or at least multiple) of your elements you are intending to store in the Array are belonging to one (physical) entity, you could create a custom Class that holds its own properties as class members, and fill your Array with a list of instances of this Class.
Moreover, if you plan to add elements to your Array, you should use a List instead, e.g. an ArrayList.
As for retaining your data, you would have to either store it in a database, or save it to a file. In either way, you will have to save it upon close of the Activity, and load it again once the Activity starts
To pass the data across activities you will need to pass them using objects you can store in an intent. Seems like the best way to handle that is to either create a PREFS file to store the data or to create an object that extends Parcelable like here:
https://stackoverflow.com/questions/18593619/android-parcelable-object-passing-to-another-activity
Parcelables are preferable assuming you need all the data in a single object, you do not want to "putExtra" a bunch of fields and you also want to be sure data can pass from one activity to another. Otherwise, a simple Util class that reads and writes to a PREFS file is the way to go:
android read/write user preferences
A database is always another option, but seems well outside the scope of your question.
I have an Android Class called Receipt, it's modeled off a typical Receipt you'd get in a retail environment and includes the following Class variables:
receipt_number
date_ordered
date_paid
item_quantity
item_type
item_sku
item_price
total_price
customer_id
customer_name
clerk_id
clerk_name
Whew... there's a lot. I'm just becoming accustom to OOP and while I love the idea of using a Receipt data Class to store the properties of a Receipt, I fear I am using an object just for the sake of using an object and not using data Classes appropriately.
Why? Well, I have another method in another Class which I feed my Receipt object to. Once fed to this other Class, the Receipt data is used to fill out a View which allows the user to edit said Receipt (data from the Receipt fills out EditText boxes which can be changed, then saved). The problem is that the code is getting ridiculous for updating a Receipt. I've got a helper method in Receipt for virtually every variable above (e.g. setClerkId(), setCustomerName(), setItemSku(), etc. etc.) and when I update a Receipt, I find myself calling all these methods and it's turning into a huge rats nest.
Surely I am missing the boat here, and probably by a long-shot. There must be a more sane way to feed in all the values of my new Receipt (it's really an update of the old object) without manually updating each variable using a helper method? I guess I'd be a little bit surprised (no, a lot surprised) if this is the correct way of doing this.
Any push in the right direction would be much appreciated!
Thanks!
You are doing it right I guess. I an object has a lot of named properties then you'll have to do a lot of get/set.
You can simplify that if you don't need to access each individually if you put them all into single (or several) ArrayList<String>/String[] or even other objects. Whatever you feel is an appropriate representation of your data.
Or you take a HashMap instead of your class and store them like so:
HashMap<String, String> hashmap = new HashMap<String, String>();
hashmap.put("receipt_number", value);
String value = hashmap.get("receipt_number");
That results in a more dynamic way to store values.
I need to save List of simple objects
class simple{
String name;
byte[] data;
}
in android. How to do this ? Do I need SQLite or there is easier way ?
Just refer serialization in java, and that makes your objects to be serialized.
see an example here,
http://www.jondev.net/articles/Android_Serialization_Example_%28Java%29
I'd use a database for that. It would make easier to get that data from different activities. Here is a great tutorial on dealing with database in Android.
I assume you can take that byte array as an INTEGER one for database type compatibility (or maybe some clever conversion to something else). The tutorial above provides you with info on the data types.